attr()

功能

获取或设置指定元素的属性值。

语法

datax.attr(elm, name, value)

参数

  • elm
    HTMLElement对象,指定对象HTML元素
  • name
    属性名
  • value
    新的属性值。如果是null,指定的属性将被删除(如.removeAttr())

范例

Example: 查找每个含有 <em> 中的 title 属性,并显示在 <div>。

<!DOCTYPE html>
<html>

<head>
    <style>
  em { color:blue; font-weight:bold; }
  div { 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>
<p>
  Once there was a <em title="huge, gigantic">large</em> dinosaur...
</p>
 
  The title of the emphasis is:<div></div>
    <script>
    require(["skylark/query","skylark/finder", "skylark/datax"], function(query,finder, datax) {
        var title=datax.attr(finder.find("em"), "title");
        query("div").text(title);
    });
    </script>
</body>

</html>