data()

功能

存储与指定元素相关的任意数据,或返回设置的值。

语法

datax.data(elm, name, value)

参数

  • elm
    HTMLElement对象,指定对象HTML元素
  • name
    数据的名称
  • value
    新的数据值

范例

Example: 从div元素储存然后找回一个值。

<!DOCTYPE html>
<html>

<head>
    <style>
    div { color: blue;}
    span { color: red;}
    </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>
        The values stored were
        <span></span> and
        <span></span>
    </div>
    <script>
    require(["skylark/query", "skylark/finder", "skylark/datax"], function(query, finder, datax) {
        var div = finder.find("div");
        datax.data(div, "test", {
            first: 16,
            last: "pizza!"
        });
        query("span:first").text(datax.data(div, "test").first);
        query("span:last").text(datax.data(div, "test").last);
    });
    </script>
</body>

</html>