jQuery慢慢啃(三)

來源:互聯網
上載者:User

標籤:

1.attr(name|properties|key,value|fn)設定或返回被選元素的屬性值。

$("img").attr("src");//擷取屬性

$("img").attr({ src: "test.jpg", alt: "Test Image" });//設定多個屬性

$("img").attr("src","test.jpg");//設定單個屬性

$("img").attr("title", function() { return this.src });//設定title值為函數傳回值

 

2.removeAttr(name)從每一個匹配的元素中刪除一個屬性

$("img").removeAttr("src");

 

3.prop(name|properties|key,value|fn)

$("input[type=‘checkbox‘]").prop("checked");//擷取元素屬性值

$("input[type=‘checkbox‘]").prop({disabled: true});//map設定元素屬性值

$("input[type=‘checkbox‘]").prop("disabled", false);//設定元素屬性值

$("input[type=‘checkbox‘]").prop("checked", function( i, val ) {  return !val;});//此處i表示元素所在索引,val代表元素的屬性值

.attr()和.prop()都不應該被用來取值/設值。使用.val()方法代替

.prop()方法應該被用來處理boolean attributes/properties以及在html(比如:window.location)中不存在的properties。其他所有的attributes(在html中你看到的那些)可以而且應該繼續使用.attr()方法來進行操作。 

 

4.removeProp(name)用來刪除由.prop()方法設定的屬性集

var $para = $("p"); $para.prop("luggageCode", 1234);

$para.removeProp("luggageCode");

 

5.addClass(class|fn)為每個匹配的元素添加指定的類名。

$("p").addClass("selected");//添加一個類

$("p").addClass("selected1 selected2");//添加2個類

$(‘ul li‘).addClass(function(index, class) {return ‘item-‘ + $(this).index();});// 對集合中的元素加上不同的類,其中函數第一個參數是索引,第二個參數是對應的原來的類名值

 

6.removeClass([class|fn])從所有匹配的元素中刪除全部或者指定的類。

$("p").removeClass("selected");

$("p").removeClass();//刪除所有類

$(‘li:last‘).removeClass(function() {return $(this).prev().attr(‘class‘);});//用函數刪除

 

7.toggleClass(class|fn[,sw])如果存在(不存在)就刪除(添加)一個類。

$("p").toggleClass("selected");//切換類

var count = 0;//根據點擊次數切換類

$("p").click(function(){$(this).toggleClass("highlight", count++ % 3 == 0);  });

$(‘div.foo‘).toggleClass(function() {//根據其他條件切換類名

if ($(this).parent().is(‘.bar‘)

{     return ‘happy‘;   }

else {     return ‘sad‘;   }

});

 

8.html([val|fn])取得第一個匹配元素的html內容。這個函數不能用於XML文檔。但可以用於XHTML文檔

$(‘p‘).html();返回p元素的內容。

$("p").html("Hello <b>world</b>!");//設定所有 p 元素的內容

 $("p").html(function(n){return "這個 p 元素的 index 是:" + n; });//使用函數來設定所有匹配元素的內容。

 

9.text([val|fn])取得所有匹配元素的內容。

$(‘p‘).text();//返回p元素的常值內容

$("p").text("Hello world!");//設定所有 p 元素的常值內容

 $("p").text(function(n){return "這個 p 元素的 index 是:" + n; });//使用函數來設定所有匹配元素的常值內容。

 

10.val([val|fn|arr])獲得匹配元素的當前值。

$("input").val();//擷取文字框中的值

$("input").val("hello world!");//設定文字框的值

$(‘input:text.items‘).val(function() {return this.value + ‘ ‘ + this.className;});//設定文字框的值

<select id="single">

  <option>Single</option>

  <option>Single2</option>

</select>

<select id="multiple" multiple="multiple">

  <option selected="selected">Multiple</option>

  <option>Multiple2</option>

  <option selected="selected">Multiple3</option>

</select><br/>

<input type="checkbox" value="check1"/> check1

<input type="checkbox" value="check2"/> check2

<input type="radio" value="radio1"/> radio1

<input type="radio" value="radio2"/> radio2

執行:

$("#single").val("Single2");

$("#multiple").val(["Multiple2", "Multiple3"]);

$("input").val(["check2", "radio1"]);

 

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.