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

attr(properties )

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

描述 (Description)

attr( properties)方法将键/值对象设置为所有匹配元素的属性。

语法 (Syntax)

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

<i>selector</i>.attr({property1:value1, property2:value2})

参数 (Parameters)

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

  • property - 这是匹配元素的CSS属性。

  • value - 这是要设置的属性的值。

例子 (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() {
            $("img").attr({ 
               src: "/images/jquery.jpg",
               title: "jQuery",
               alt: "jQuery Logo"
            });
         });
      </script>	
   </head>
   <body>
      <div class = "division" id = "divid">
         <p>Following is the logo of jQuery</p>
         <img src="/jquery/images/jquery-mini-logo.jpg" title="None" alt="None" />
      </div>
   </body>
</html>