詳談jQuery操縱DOM元素屬性 attr()和removeAtrr()方法
jQuery中操縱元素屬性的方法:
attr(): 讀或者寫匹配元素的屬性值.
removeAttr(): 從匹配的元素中移除指定的屬性.
attr()方法 讀操作
attr()讀操作. 讀取的是匹配元素中第一個(gè)元素的指定屬性值.
格式: .attr(attributeName),返回值類型:String.讀取不存在的屬性會(huì)返回undefined.
注意選擇器的選擇結(jié)果可能是一個(gè)集合,這里僅僅獲取的是集合中第一個(gè)元素的該屬性值.
看例子:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("button").click(function () {
alert($("p").attr("title"));//獲取屬性
// this code can only get the first element's attribute.
});
});
</script>
</head>
<body>
<p title="title1">paragraph 1</p>
<p title="title2">paragraph 2</p>
<br/>
<button>get title</button>
</body>
</html>
運(yùn)行結(jié)果:彈框顯示: title1.
想要分別獲取每一個(gè)元素的屬性,需要使用jQuery的循環(huán)結(jié)構(gòu),比如.each()或.map()方法.
上面的例子可以改成:
<script type="text/javascript">
$(document).ready(function () {
$("button").click(function () {
//get attribute for every element in selection.
$("p").each(function () {
alert($(this).attr("title"));
});
});
});
</script>
即可分別獲取每個(gè)元素的屬性.
attr()方法 寫操作
attr()寫操作. 為匹配元素的一個(gè)或多個(gè)屬性賦值.
一般格式: .attr(attributeName, value), 即為屬性設(shè)置value.
返回值類型:jQuery.也即支持鏈?zhǔn)椒椒ㄕ{(diào)用.
執(zhí)行寫操作的時(shí)候,如果指定的屬性名不存在,將會(huì)增加一個(gè)該名字的屬性,即增加自定義屬性,其名為屬性名,其值為value值.
寫屬性是為匹配集合中的每一個(gè)元素都進(jìn)行操作的,見例子:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("p").attr("title","Hello World");
});
});
</script>
</head>
<body>
<input type="button" id="button1" value="button1"></input>
<p>This is a paragraph.</p>
<div>This is a div.</div>
<p>This is another paragraph.</p>
<div>This is another div.</div>
</body>
</html>
點(diǎn)擊按鈕之后所有的p都加上了title="Hello World”的屬性.
寫操作還有以下兩種格式:
.attr(attributes)和.attr(attributeName, function).
下面分別介紹.
.attr(attributes):
這里attributes類型是PlainObject,可以用于一次性設(shè)置多個(gè)屬性.
什么是PlainObject呢,簡單理解就是大括號(hào)包圍的鍵值對(duì)序列.可以參考問后鏈接說明.
鍵和值之間用冒號(hào)(:)分隔,每個(gè)鍵值對(duì)之間用逗號(hào)(,)分隔.
注意: 設(shè)置多個(gè)屬性值時(shí),屬性名的引號(hào)是可選的(可以有,也可以沒有).但是class屬性是個(gè)例外,必須加上引號(hào).
例子:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#button1").click(function () {
$("p").attr("title", "Hello World");
});
$("#button2").click(function () {
$("div").attr({"title": "some title for div", "hello": "World"});
});
});
</script>
</head>
<body>
<input type="button" id="button1" value="button1"></input>
<input type="button" id="button2" value="button2"></input>
<p>This is a paragraph.</p>
<div>This is a div.</div>
<p>This is another paragraph.</p>
<div>This is another div.</div>
</body>
</html>
點(diǎn)擊兩個(gè)按鈕之后,元素變?yōu)?
其中<div>的hello是一個(gè)新增的自定義屬性,其value為World.
.attr(attributeName, function(index, oldValue)):
使用一個(gè)function來設(shè)置屬性值.function的第一個(gè)參數(shù)是index,第二個(gè)參數(shù)是該屬性之前的值.
看例子:
<!DOCTYPE html>
<html>
<head>
<style>
div {
color: blue;
}
span {
color: red;
}
b {
font-weight: bolder;
}
</style>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("div")
.attr("id", function (index, oldAttr) {
if (oldAttr) {
return "div-id" + index + oldAttr;
} else {
return "div-id" + index;
}
})
.each(function () {
$("span", this).html("(id = '<b>" + this.id + "</b>')");
});
});
</script>
</head>
<body>
<div id="someId">Zero-th <span></span></div>
<div>First <span></span></div>
<div>Second <span></span></div>
</body>
</html>
上面的例子,對(duì)應(yīng)的頁面結(jié)果如下:
當(dāng)使用一個(gè)方法來設(shè)定屬性值的時(shí)候,如果這個(gè)set的function沒有返回值,或者返回了undefined,當(dāng)前的值是不會(huì)被改變的.
即操作會(huì)被忽略.
還是上面的例子,attr()其中的function返回undefined:
如下:
<script type="text/javascript">
$(document).ready(function () {
$("div").attr("id", function (index, oldAttr) {
return undefined;
}).each(function () {
$("span", this).html("(id = '<b>" + this.id + "</b>')");
});
});
</script>
返回的頁面效果如下:
即沒有進(jìn)行任何修改操作,還是保持原來的屬性值.
注意:jQuery不能修改<input>和<button>的type屬性,如果修改會(huì)在瀏覽器報(bào)錯(cuò).
這是因?yàn)镮E瀏覽器中不能修改type屬性.
removeAttr()方法
移除匹配元素集合中每一個(gè)元素的指定屬性.
removeAttr()方法調(diào)用的是JavaScript的removeAttribute()方法,但是它能用jQuery對(duì)象直接調(diào)用,并且它考慮到并處理了各個(gè)瀏覽器上的屬性名稱可能不統(tǒng)一的問題.
例子:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("input[type=button]").click(function () {
$("div").removeAttr("title");
});
});
</script>
</head>
<body>
<input type="button" value="ClickMe"></input>
<div title="hello">Zero</div>
</body>
</html>
點(diǎn)擊按鈕后,<div>的title屬性會(huì)被刪除.
注意: 用removeAttr()移除onclick在IE6-8上都不會(huì)起作用,為了避免這個(gè)問題,應(yīng)該使用.prop()方法.
比如:
$element.prop( "onclick", null );
console.log( "onclick property: ", $element[ 0 ].onclick );
相關(guān)文章
基于jQuery Ajax實(shí)現(xiàn)上傳文件
這篇文章主要為大家詳細(xì)介紹了jQuery Ajax上傳文件的相關(guān)代碼,需要的朋友可以參考下2016-03-03jQuery HTML獲取內(nèi)容和屬性操作實(shí)例分析
這篇文章主要介紹了jQuery HTML獲取內(nèi)容和屬性操作,結(jié)合實(shí)例形式分析了jQuery HTML獲取內(nèi)容和屬性相關(guān)函數(shù)用法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05用Jquery實(shí)現(xiàn)多級(jí)下拉框無刷新的聯(lián)動(dòng)
需要實(shí)現(xiàn):院系,專業(yè),年級(jí),班級(jí)。聯(lián)動(dòng)無刷新,其中院系,專業(yè),年級(jí),班級(jí)屬于數(shù)據(jù)庫中的字典表,有后臺(tái)維護(hù),隨時(shí)可能變化2010-12-12jquery插件treegrid樹狀表格的使用方法詳解(.Net平臺(tái))
本文主要介紹了jquery插件treegrid樹狀表格的使用方法,具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01jQuery中將json數(shù)據(jù)顯示到頁面表格的方法
今天小編就為大家分享一篇jQuery中將json數(shù)據(jù)顯示到頁面表格的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05jquery實(shí)現(xiàn)隱藏在左側(cè)的彈性彈出菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)隱藏在左側(cè)的彈性彈出菜單效果,涉及jQuery動(dòng)態(tài)操作頁面元素的顯示及隱藏技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09jQuery居中元素scrollleft計(jì)算方法示例
這篇文章主要介紹了jQuery居中元素scrollleft計(jì)算方法,結(jié)合實(shí)例形式分析了jQuery獲取及計(jì)算頁面滾動(dòng)元素的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01