当前位置: 首页 > 文档资料 > jQuery 入门教程 >

attr(key, fn )

优质
小牛编辑
115浏览
2023-12-01

描述 (Description)

attr( key, func )方法在所有匹配的元素上将单个属性设置为计算值。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.attr( key, func )

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • key - 要设置的属性的名称。

  • func - 将值返回到set的函数。 这个函数有一个参数,它是当前元素的索引。

例子 (Example)

以下示例将为每个表创建边框 -

<html>
   <head>
      <title>The Selecter Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("table").attr("border", function(index) {
               return "4px";
            })
         });
      </script>	
   </head>
   <body>
      <table>
         <tr><td>This is first table</td></tr>
      </table>
      <table>
         <tr><td>This is second table</td></tr>
      </table>
      <table>
         <tr><td>This is third table</td></tr>
      </table>	
   </body>
</html>