Jquery屬性(一) -- Attr

屬性


[1]  attr( name )      Returns: Object
      說明: 獲取第一個匹配元素的屬性值。該方法使得獲取第一個匹配元素的屬性值變得非常容易。如果一個元素沒有設置相應的屬性,則該方法返回undefined,屬性包括:title, alt, src, href, width, style等等。
      

Js代碼 複製代碼
  1. var title = $("em").attr("title");  
var title = $("em").attr("title");


       name: String -- 用於獲取屬性值的屬性名。


[2]  attr( properties )      Returns: jQuery
      說明: 爲每個匹配的元素設置一組鍵/值對象的屬性(列表)。這是一種在所有匹配元素中批量設置很多屬性的最佳方式。 注意,如果你要設置對象的class屬性,你必須使用'className' 作爲屬性名。或者你可以直接使用.addClass( class ) 和 .removeClass( class ).
      

Js代碼 複製代碼
  1. $("img").attr({    
  2.       src: "/images/hat.gif",   
  3.       title: "jQuery",   
  4.       alt: "jQuery Logo"  
  5.  });  
$("img").attr({ 
      src: "/images/hat.gif",
      title: "jQuery",
      alt: "jQuery Logo"
 });


       name: String -- 用於獲取屬性值的屬性名。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章