欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery 學(xué)習(xí)之二 屬性相關(guān)

 更新時間:2010年11月23日 16:54:41   作者:  
jquery 學(xué)習(xí)之二 屬性相關(guān)資料,學(xué)習(xí)jquery的朋友可以參考下。
attr(name)
取得第一個匹配元素的屬性值。通過這個方法可以方便地從第一個匹配元素中獲取一個屬性的值。如果元素沒有相應(yīng)屬性,則返回 undefined 。
Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned.
返回值
Object

參數(shù)
name (String) : 屬性名稱

示例
返回文檔中第一個圖像的src屬性值。

HTML 代碼:

<img src="test.jpg"/>
jQuery 代碼:

$("img").attr("src");
結(jié)果:

test.jpg
---------------------------------------------------------------------------------------------------------------------------------------
attr(properties)
將一個“名/值”形式的對象設(shè)置為所有匹配元素的屬性。
這是一種在所有匹配元素中批量設(shè)置很多屬性的最佳方式。 注意,如果你要設(shè)置對象的class屬性,你必須使用'className' 作為屬性名?;蛘吣憧梢灾苯邮褂?addClass( class ) 和 .removeClass( class ).
Set a key/value object as properties to all matched elements.
This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ).
返回值
jQuery

參數(shù)
properties (Map) : 作為屬性的“名/值對”對象

示例
為所有圖像設(shè)置src和alt屬性。

HTML 代碼:

<img/>
jQuery 代碼:

$("img").attr({ src: "test.jpg", alt: "Test Image" });
結(jié)果:

[ <img src= "test.jpg" alt:="Test Image" /> ]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,value)
為所有匹配的元素設(shè)置一個屬性值。
Set a single property to a value, on all matched elements.
返回值
jQuery

參數(shù)
key (String) : 屬性名稱

value (Object) : 屬性值

示例
為所有圖像設(shè)置src屬性。

HTML 代碼:

<img/>
<img/>
jQuery 代碼:

$("img").attr("src","test.jpg");
結(jié)果:

[ <img src= "test.jpg" /> , <img src= "test.jpg" /> ]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,fn)
為所有匹配的元素設(shè)置一個計算的屬性值。
不提供值,而是提供一個函數(shù),由這個函數(shù)計算的值作為屬性值。
Set a single property to a computed value, on all matched elements.
Instead of supplying a string value as described 'above', a function is provided that computes the value.
返回值
jQuery

參數(shù)
key (String) : 屬性名稱

fn (Function) : 返回值的函數(shù) 范圍:當(dāng)前元素, 參數(shù): 當(dāng)前元素的索引值

示例
把src屬性的值設(shè)置為title屬性的值。

HTML 代碼:

<img src="test.jpg"/>
jQuery 代碼:

$("img").attr("title", function() { return this.src });
結(jié)果:

<img src="test.jpg" title="test.jpg" />
---------------------------------------------------------------------------------------------------------------------------------------
removeAttr(name)
從每一個匹配的元素中刪除一個屬性
Remove an attribute from each of the matched elements.
返回值
jQuery

參數(shù)
name (String) : 要刪除的屬性名

示例
將文檔中圖像的src屬性刪除

HTML 代碼:

<img src="test.jpg"/>
jQuery 代碼:

$("img").removeAttr("src");
結(jié)果:

[ <img /> ]

相關(guān)文章

最新評論