jQuery手冊中的屬性和css部分

來源:互聯網
上載者:User

標籤:

屬性:1、attr(name|properties|key,value|fn)

設定或返回被選元素的屬性值。

//參數name 描述:返迴文檔中所有映像的src屬性值。$("img").attr("src");
//參數properties 描述:為所有映像設定src和alt屬性。$("img").attr({ src: "test.jpg", alt: "Test Image" });
//參數key,value 描述:為所有映像設定src屬性。$("img").attr("src","test.jpg");
//參數key,回呼函數 描述:把src屬性的值設定為img1.png , ...    $(function(){        //回呼函數(index下標)(src1可自訂,是src屬性的值)        $(‘img‘).attr(‘src‘,function(index,src1){        this.src = "img"+(index+1)+".png";        });    })//結果:      src="img1.png"      src="img2.png"      src="img3.png"      ...
2、removeAttr(name)

從每一個匹配的元素中刪除一個屬性

//將文檔中映像的src屬性刪除<img src="test.jpg"/>$("img").removeAttr("src");

 

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

擷取在匹配的元素集中的第一個元素的屬性值。 

//參數name 描述:   // 選中複選框為true,沒選中為false$("input[type=‘checkbox‘]").prop("checked");    //參數properties 描述:       // 禁用頁面上的所有複選框。    $("input[type=‘checkbox‘]").prop({disabled: true});        //參數key,value 描述:           // 禁用和選中所有頁面上的複選框。      $("input[type=‘checkbox‘]").prop("disabled", false);      $("input[type=‘checkbox‘]").prop("checked", true);            //參數key,回呼函數 描述:              //  通過函數來設定所有頁面上的複選框被選中。                  <input type="checkbox"  name="" id="" value="" />  //結果,選中勾上              <input type="checkbox" checked="checked" name="" id="" value="" />    //結果,沒勾上              <input type="checkbox" checked="checked" name="" id="" value="" />    //結果,沒勾上            $("input[type=‘checkbox‘]").prop("checked", function( i, val ) {              return !val;            });  

  

4、removeProp(name)

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

//設定一個段落數字屬性,然後將其刪除。<p></p>var $para = $("p");$para.prop("luggageCode", 1234);$para.append("The secret luggage code is: ", String($para.prop("luggageCode")), ". "+"<br>");$para.removeProp("luggageCode");$para.append("Now the secret luggage code is: ", String($para.prop("luggageCode")), ". ");//結果:The secret luggage code is: 1234. 
Now the secret luggage code is: undefined.

 

5、addClass(class|fn)

為每個匹配的元素添加指定的類名。

//參數class 描述:    為匹配的元素加上 ‘selected‘ 類$("p").addClass("selected");$("p").addClass("selected1 selected2");
//回呼函數 描述:    給最後一個class加上一個"item-2"的類<ul>      <li>Hello</li>      <li>Hello</li>      <li>Hello</li></ul>$(‘ul li:last‘).addClass(function() {  return ‘item-‘ + $(this).index();});

  

6、傳回值:jQueryremoveClass([class|fn])

從所有匹配的元素中刪除全部或者指定的類。

//參數class 描述:   從匹配的元素中刪除 ‘selected‘ 類$("p").removeClass("selected");//參數class 描述:    刪除匹配元素的所有類$("p").removeClass();//回呼函數描述:    刪除最後一個元素上與前面重複的class$(‘li:last‘).removeClass(function() {    return $(this).prev().attr(‘class‘);});

   

7、toggleClass(class|fn[,sw])

 如果存在(不存在)就刪除(添加)一個類。

//參數class 描述:    為匹配的元素切換 ‘selected‘ 類$("p").toggleClass("selected");//參數class,switch 描述:    每點擊三下加上一次 ‘highlight‘ 類    <strong>jQuery 代碼:</strong>  var count = 0;  $("p").click(function(){      $(this).toggleClass("highlight", count++ % 3 == 0);  });//回呼函數 描述:    根據父元素來設定class屬性$(‘div.foo‘).toggleClass(function() {  if ($(this).parent().is(‘.bar‘) {    return ‘happy‘;  } else {    return ‘sad‘;  }});

  

8、html([val|fn])

取得第一個匹配元素的html內容。這個函數不能用於XML文檔。但可以用於XHTML文檔。

在一個 HTML 文檔中, 我們可以使用 .html() 方法來擷取任意一個元素的內容。 如果選取器匹配多於一個的元素,那麼只有第一個匹配元素的 HTML 內容會被擷取。

//無參數 描述:    返回p元素的內容。$(‘p‘).html();//參數val 描述:    設定所有 p 元素的內容$("p").html("Hello <b>world</b>!");回呼函數描述:使用函數來設定所有匹配元素的內容。jQuery 代碼:$("p").html(function(n){    return "這個 p 元素的 index 是:" + n;    });

 

9、text([val|fn])

取得所有匹配元素的內容。

結果是由所有匹配元素包含的常值內容組合起來的文本。這個方法對HTML和XML文檔都有效。

 

//無參數 描述:    返回p元素的常值內容。$(‘p‘).text();//參數val 描述:    設定所有 p 元素的常值內容$("p").text("Hello world!");//回呼函數 描述:    使用函數來設定所有匹配元素的常值內容。$("p").text(function(n){    return "這個 p 元素的 index 是:" + n;    });

  

10、val([val|fn|arr])

獲得匹配元素的當前值。

在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多選,將返回一個數組,其包含所選的值。

//無參數 描述:    擷取文字框中的值$("input").val();//參數val 描述:    設定文字框的值$("input").val("hello world!");//回呼函數 描述:    設定文字框的值$(‘input:text.items‘).val(function() {  return this.value + ‘ ‘ + this.className;});//參數array 描述:    選中check2,radio1<input type="checkbox" value="check1"/> check1<input type="checkbox" value="check2"/> check2<input type="radio" value="radio1"/> radio1<input type="radio" value="radio2"/> radio2$("input").val(["check2", "radio1"]);

  

css部分 11、css(name|pro|[,val|fn])

訪問匹配元素的樣式屬性。

//參數name 描述:    取得第一個段落的color樣式屬性的值。$("p").css("color");//參數properties 描述:    將所有段落的字型顏色設為紅色並且背景為藍色。$("p").css({ "color": "#ff0011", "background": "blue" });//參數name,value 描述:    將所有段落字型設為紅色$("p").css("color","red");//參數name,回呼函數 描述:    逐漸增加div的大小  $("div").click(function() {    $(this).css({      width: function(index, value) {        return parseFloat(value) * 1.2;      },       height: function(index, value) {        return parseFloat(value) * 1.2;      }    });  });

 

 

12、jQuery.cssHooks

直接向 jQuery 中添加鉤子,用於覆蓋設定或擷取特定 CSS 屬性時的方法,目的是為了標準化 CSS 屬性名稱或建立自訂屬性。

$.cssHooks 對象提供了一種通過定義函數來擷取或設定特定 CSS 值的方法。可以用它來建立新的 cssHooks 用於標準化 CSS3 功能,例如,盒子陰影(box shadows)及漸層(gradients)。

例如,某些基於 Webkit 的瀏覽器會使用 -webkit-border-radius 來設定對象的 border-radius,然而,早先版本的 Firefox 則使用 -moz-border-radius。cssHook 就可以將這些不同的寫法進行標準化,從而讓 .css() 可以使用統一的標準化屬性名稱(border-radius 或對應的 DOM 屬性寫法 borderRadius)。

該方法除了提供了對特定樣式的處理可以採用更加細緻的控制外,$.cssHooks 同時還擴充了 .animate() 方法上的屬性集

 

13、offset([coordinates])

擷取匹配元素在當前視口的相對位移。

返回的對象包含兩個整型屬性:top 和 left,以像素計。此方法只對可見元素有效。

//擷取第二段的位移<p>Hello</p><p>2nd Paragraph</p>var p = $("p:last");var offset = p.offset();p.html( "left: " + offset.left + ", top: " + offset.top );

  

14、position()

擷取匹配元素相對父元素的位移。

返回的對象包含兩個整型屬性:top 和 left。為精確計算結果,請在補白、邊框和填充屬性上使用像素單位。此方法只對可見元素有效。

//擷取第一段的位移<p>Hello</p><p>2nd Paragraph</p>var p = $("p:first");var position = p.position();$("p:last").html( "left: " + position.left + ", top: " + position.top );

  

15、scrollTop([val])

擷取匹配元素相對捲軸頂部的位移。

此方法對可見和隱藏元素均有效。

//無參數描述:    // 擷取第一段相對捲軸頂部的位移<p>Hello</p><p>2nd Paragraph</p>var p = $("p:first");$("p:last").text( "scrollTop:" + p.scrollTop() );//結果:<p>Hello</p><p>scrollTop: 0</p>//參數val 描述:      //設定相對捲軸頂部的位移$("div.demo").scrollTop(300);

 

16、scrollLeft([val])

擷取匹配元素相對捲軸左側的位移。

此方法對可見和隱藏元素均有效。

//無參數描述:   // 擷取第一段相對捲軸左側的位移<p>Hello</p><p>2nd Paragraph</p>var p = $("p:first");$("p:last").text( "scrollLeft:" + p.scrollLeft() );//參數val 描述:   // 設定相對捲軸左側的位移$("div.demo").scrollLeft(300);

  

17、height([val|fn])

取得匹配元素當前計算的高度值(px)。

在 jQuery 1.2 以後可以用來擷取 window 和 document 的高

//無參數描述:    //擷取第一段的高$("p").height();//參數val 描述:   // 把所有段落的高設為 20:$("p").height(20);//參數function(index, height) 描述:  //  以 10 像素的幅度增加 p 元素的高度jQuery 代碼:  $("button").click(function(){    $("p").height(function(n,c){    return c+10;    });  });

  

18、width([val|fn])

取得第一個匹配元素當前計算的寬度值(px)。

 

19、innerHeight()

擷取第一個匹配元素內部地區高度(包括補白、不包括邊框)。

此方法對可見和隱藏元素均有效。

//擷取第一段落內部地區高度。<p>Hello</p><p>2nd Paragraph</p>var p = $("p:first");$("p:last").text( "innerHeight:" + p.innerHeight() );

  

20、innerWidth()

擷取第一個匹配元素內部地區寬度(包括補白、不包括邊框)。

此方法對可見和隱藏元素均有效。

 

21、outerHeight([options])

擷取第一個匹配元素外部高度(預設包括補白和邊框)。

此方法對可見和隱藏元素均有效。

//擷取第一段落外部高度。<p>Hello</p><p>2nd Paragraph</p>var p = $("p:first");$("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight(true):" + p.outerHeight(true) );

  

22、outerWidth([options])

擷取第一個匹配元素外部寬度(預設包括補白和邊框)。

此方法對可見和隱藏元素均有效。

 

 

 

 

 

 

 

 

 

 

 

 

  

jQuery手冊中的屬性和css部分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.