国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-21 00:06:00
字體:
供稿:網(wǎng)友
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 /> ]
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汕尾市| 西昌市| 景德镇市| 治多县| 温州市| 大关县| 西安市| 吉隆县| 黄浦区| 乐都县| 虹口区| 米易县| 娱乐| 鹤峰县| 喀喇| 西华县| 内黄县| 乐清市| 齐齐哈尔市| 纳雍县| 宁波市| 日照市| 勐海县| 酉阳| 华池县| 玉林市| 黔东| 光山县| 望江县| 宜都市| 翁源县| 都昌县| 兖州市| 曲沃县| 镇坪县| 长春市| 沁源县| 北流市| 收藏| 邢台县| 翼城县|