each()

功能

遍历一个对象(普通对象或数组),为每个元素执行一个函数。

语法

langx.each(obj, callback)

参数

  • obj
    要迭代的对象
  • callback
    将在每个元素上执行的函数

范例

Example: 遍历<div>元素,如果 style.color 为 blue 赋值 null,否则 style.color 值 blue。

<!DOCTYPE html>
<html>

<head>
    <style>
    div {  color: red;text-align: center;cursor: pointer;font-weight: bolder;width: 300px;}
    </style>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script>
    <script>
    require.config({
        baseUrl: "./",
        packages: [{
            name: "skylark",
            location: "../../../src/skylark"
        }, ]
    });
    </script>
</head>

<body>
    <div>Click here</div>
    <div>to iterate through</div>
    <div>these divs.</div>
    <script>
    require(["skylark/query", "skylark/langx"], function(query, langx) {
        query(document.body).on("click", function() {
            langx.each(query("div"), function(i) {
                if (this.style.color != "blue") {
                    this.style.color = "blue";
                } else {
                    this.style.color = "";
                }
            });
        });
    });
    </script>
</body>

</html>