jquery 属性 attr (key,fn)

 

attr(key,fn)

将单一属性及属性值注入到匹配集合里每个元素中。其中属性值以函数的方式处理后得到。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("div").attr("id", function (arr) {
          return "div-id" + arr;
        })
        .each(function () {
          $("span", this).html("(ID = '<b>" + this.id + "</b>')");
        });

  });
  </script>
  <style>
  div { color:blue; }
  span { color:red; }
  b { font-weight:bolder; }
  </style>
</head>
<body>
  <div>Zero-th <span></span></div>
  <div>First <span></span></div>
  <div>Second <span></span></div>
</body>
</html>

 

$("div").attr("id", function (arr) {
将function处理后返回值注入到匹配div集合的每个div元素id属性中。以下是处理后返回值

"div-id" + arr;//arr为元素索引

你可能感兴趣的:(html,jquery)