jQuery特性操作

來源:互聯網
上載者:User

標籤:資訊   元素   

前面的話

  每個元素都有一個或者多個特性,這些特性的用途就是給出相應元素或者其內容的附加資訊。操作特性的DOM方法主要有3個:getAttribute()方法、setAttribute()方法和removeAttribute()方法,而在jQuery中用一個attr()與removeAttr()就可以全部搞定了,包括相容問題。本文將介紹jQuery中的特性操作

 

擷取特性

  jQuery中用attr()方法來擷取和設定特性,attr是attribute(特性)的縮寫,在jQuery DOM操作中會經常用到attr()方法

attr(attributeName)

  attr(傳入特性名):擷取特性的值,相當於DOM中的getAttribute()

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="test"></div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>console.log(test.getAttribute(‘id‘));//‘test‘    console.log($(‘#test‘).attr(‘id‘));//‘test‘</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

  [注意]attr()方法只擷取第一個匹配元素的屬性值。要擷取每個單獨的元素的屬性值,我們依靠jQuery的.each()或者.map()方法迴圈

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div class="test" id="ele1">元素一</div><div class="test" id="ele2">元素二</div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>console.log($(‘.test‘).attr(‘id‘));//‘test‘$(‘.test‘).each(function(index) {  console.log(index+":"+$(this).attr(‘id‘));//‘1:ele1 2:ele2‘});</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

prop()

  屬性(property)和特性(attribute)是不同的。屬性是DOM節點的屬性,而特性是HTML標籤的特性

  [注意]關於屬性和特性的區別的詳細資料移步至此

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="test" data="abc"></div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>test.data = 123;//IE8-瀏覽器返回123,其他瀏覽器返回‘abc‘console.log(test.getAttribute(‘data‘))console.log(test.data)//123//IE8-瀏覽器返回123,其他瀏覽器返回‘abc‘console.log($(‘#test‘).attr(‘data‘))console.log($(‘#test‘).prop(‘data‘))//123</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

  由上面代碼可知,jQuery並沒有解決低版本IE瀏覽器屬性和特性混淆的問題

 

設定特性

  設定特性雖然依然使用attr()方法,但卻有3種方式

【1】attr(attributeName,value)

  attr(特性名, 特性值):設定特性的值,相當於DOM中的setAttribute()

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="test"></div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>test.setAttribute(‘title‘,‘abc‘);console.log(test.getAttribute(‘title‘))//‘abc‘$(‘#test‘).attr(‘title‘,‘123‘);    console.log($(‘#test‘).attr(‘title‘));//‘123‘</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

  jQuery禁止改變一個<input>或<button>元素的type特性,會靜默失敗。因為IE8-不會允許改變<input>或者<button>元素的type特性,靜默失敗

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<input id="test" type="text"><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>test.setAttribute(‘type‘,‘button‘);$(‘#test‘).attr(‘type‘,‘button‘);    </script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

【2】attr(attributes)

  attr(attributes):給指定元素設定多個特性值,即{特性名一: “特性值一”,特性名二:“特性值二”,…}

  當設定多個特性,包裹特性名的引號是可選的

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="test"></div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>test.setAttribute(‘title‘,‘abc‘);test.setAttribute(‘a‘,‘abc‘);console.log(test.getAttribute(‘title‘))//‘abc‘console.log(test.getAttribute(‘a‘))//‘abc‘$(‘#test‘).attr({    title: ‘123‘,    a: ‘123‘});    console.log($(‘#test‘).attr(‘title‘));//‘123‘console.log($(‘#test‘).attr(‘a‘));//‘123‘</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

  [注意]設定樣式名“class”特性時,必須使用引號。否則IE8-瀏覽器下會報錯

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="test"></div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>$(‘#test‘).attr({    class: ‘test‘});    //IE8-瀏覽器會報錯,其他瀏覽器輸出‘test‘console.log($(‘#test‘).attr(‘class‘));</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

【3】attr(attributeName,function(index,attr))

  attr(特性名,函數值):通過使用一個函數來設定屬性,可以根據該元素上的其它屬性值返回最終所需的屬性值

  函數中的index表示元素在匹配集合中的索引位置,html表示元素原來的HTML內容,this指向當前的元素,函數返回用來設定的值

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div class="test" id="ele1" title="元素">元素一</div><div class="test" id="ele2" title="元素" >元素二</div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>$(‘.test‘).attr(‘title‘,function(index,attr){    return attr + this.className +index})console.log($(‘#ele1‘).attr(‘title‘));//元素test0console.log($(‘#ele2‘).attr(‘title‘));//元素test1</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

  如果用javascript實作類別似的效果,實際上就是字串串連

  [注意]IE8-瀏覽器不支援getElementsByClassName()方法

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div class="test" id="ele1" title="元素">元素一</div><div class="test" id="ele2" title="元素" >元素二</div><script>var classTest = document.getElementsByClassName(‘test‘);for(var i = 0; i < classTest.length; i++){    classTest[i].title = classTest[i].title + classTest[i].className + i;}console.log(ele1.title);//元素test0console.log(ele2.title);//元素test1</script>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

 

刪除特性

removeAttr(attributeName)

  removeAttr()方法使用原生的removeAttribute()函數,但是它的優點是可以直接在一個jQuery 對象上調用該方法,並且它解決了跨瀏覽器的特性名不同的問題

  要移除的屬性名稱從1.7版本開始,可以是一個空格分隔的屬性列表

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;float:left;border:none;" />

<div id="ele1" title="元素" data="value">元素</div><script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script>console.log($(‘#ele1‘).attr(‘title‘));//‘元素‘console.log($(‘#ele1‘).attr(‘data‘));//‘value‘$(‘#ele1‘).removeAttr(‘title data‘);console.log($(‘#ele1‘).attr(‘title‘));//undefinedconsole.log($(‘#ele1‘).attr(‘data‘));//undefined</script>



jQuery特性操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.