Jquery屬性&css 大全

來源:互聯網
上載者:User

標籤:

 

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

樣本

返迴文檔中所有映像的src屬性值。

jQuery 代碼:

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

為所有映像設定src和alt屬性。

jQuery 代碼:

$("img").attr({ src: "test.jpg", alt: "Test Image" });

為所有映像設定src屬性。

jQuery 代碼:

$("img").attr("src","test.jpg");

把src屬性的值設定為title屬性的值。

jQuery 代碼:

$("img").attr("title", function() { return this.src });

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

樣本

將文檔中映像的src屬性刪除

HTML 程式碼:

<img src="test.jpg"/>

jQuery 代碼:

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

結果:

[ <img /> ]

3. prop(name|properties|key,value|fn) 擷取在匹配的元素集中的第一個元素的屬性值。

樣本

選中複選框為true,沒選中為false

jQuery 代碼:

$("input[type=‘checkbox‘]").prop("checked");

禁用頁面上的所有複選框。

jQuery 代碼:

$("input[type=‘checkbox‘]").prop({disabled: true});

禁用和選中所有頁面上的複選框。

jQuery 代碼:

$("input[type=‘checkbox‘]").prop("disabled", false);

$("input[type=‘checkbox‘]").prop("checked", true);

通過函數來設定所有頁面上的複選框被選中。

jQuery 代碼:

$("input[type=‘checkbox‘]").prop("checked", function( i, val ) {

  return !val;

});

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

樣本

設定一個段落數字屬性,然後將其刪除。

HTML 程式碼:

<p> </p>

jQuery 代碼:

var $para = $("p");

$para.prop("luggageCode", 1234);

$para.append("The secret luggage code is: ", String($para.prop("luggageCode")), ". ");

$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) 為每個匹配的元素添加指定的類名。

樣本

為匹配的元素加上 ‘selected‘ 類

jQuery 代碼:

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

$("p").addClass("selected1 selected2");

回呼函數 描述:

給li加上不同的class

HTML 程式碼:

<ul>

      <li>Hello</li>

      <li>Hello</li>

      <li>Hello</li>

</ul>

jQuery 代碼:

$(‘ul li:last‘).addClass(function() {

 

  return ‘item-‘ + $(this).index();

});

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

樣本

從匹配的元素中刪除 ‘selected‘ 類

jQuery 代碼:

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

刪除匹配元素的所有類

jQuery 代碼:

$("p").removeClass();

回呼函數描述:

刪除最後一個元素上與前面重複的class

jQuery 代碼:

$(‘li:last‘).removeClass(function() {

    return $(this).prev().attr(‘class‘);

});

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

樣本

為匹配的元素切換 ‘selected‘ 類

jQuery 代碼:

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

每點擊三下加上一次 ‘highlight‘ 類

HTML 程式碼:

<strong>jQuery 代碼:</strong>

jQuery 代碼:

  var count = 0;

  $("p").click(function(){

      $(this).toggleClass("highlight", count++ % 3 == 0);

  });

回呼函數 描述:

根據父元素來設定class屬性

jQuery 代碼:

$(‘div.foo‘).toggleClass(function() {

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

    return ‘happy‘;

  } else {

    return ‘sad‘;

  }

});

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

樣本

返回p元素的內容。

jQuery 代碼:

$(‘p‘).html();

設定所有 p 元素的內容

jQuery 代碼:

$("p").html("Hello <b>world</b>!");

回呼函數描述:

使用函數來設定所有匹配元素的內容。

jQuery 代碼:

$("p").html(function(n){

    return "這個 p 元素的 index 是:" + n;

});

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

樣本

返回p元素的常值內容。

jQuery 代碼:

$(‘p‘).text();

設定所有 p 元素的常值內容

jQuery 代碼:

$("p").text("Hello world!");

回呼函數 描述:

使用函數來設定所有匹配元素的常值內容。

jQuery 代碼:

$("p").text(function(n){

    return "這個 p 元素的 index 是:" + n;

    });

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

樣本

擷取文字框中的值

jQuery 代碼:

$("input").val();

設定文字框的值

jQuery 代碼:

$("input").val("hello world!");

回呼函數 描述:

設定文字框的值

jQuery 代碼:

$(‘input:text.items‘).val(function() {

  return this.value + ‘ ‘ + this.className;

});

11. css(name|pro|[,val|fn]) 訪問匹配元素的樣式屬性。

樣本

取得第一個段落的color樣式屬性的值。

jQuery 代碼:

$("p").css("color");

將所有段落的字型顏色設為紅色並且背景為藍色。

jQuery 代碼:

$("p").css({ "color": "#ff0011", "background": "blue" });

將所有段落字型設為紅色

jQuery 代碼:

$("p").css("color","red");

逐漸增加div的大小

jQuery 代碼:

  $("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 屬性名稱或建立自訂屬性。

定義一個新的 cssHook 十分簡單。下面的模板可以方便您建立自己的 cssHook:

(function($) {

    // first, check to see if cssHooks are supported

    if ( !$.cssHooks ) {

      // if not, output an error message

      throw("jQuery 1.4.3 or above is required for this plugin to work");

      return;

    }

      $.cssHooks["someCSSProp"] = {

      get: function( elem, computed, extra ) {

        // handle getting the CSS property

      },      set: function( elem, value ) {

        // handle setting the CSS value

     }

    };

  })(jQuery); 

 

            // add property to $.support so it can be accessed elsewhere

      $.support[ prop ] = supportedProp;

            return supportedProp;

    }

      // call the function, e.g. testing for "border-radius" support:

    styleSupport( "borderRadius" );

  })(jQuery); 

13. offset([coordinates]) 擷取匹配元素在當前視口的相對位移。

樣本

擷取第二段的位移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:last");

var offset = p.offset();

p.html( "left: " + offset.left + ", top: " + offset.top );

結果:

<p>Hello</p><p>left: 0, top: 35</p>

擷取第二段的位移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

$("p:last").offset({ top: 10, left: 30 });

14. position() 擷取匹配元素相對父元素的位移。

樣本

描述:

擷取第一段的位移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

var position = p.position();

$("p:last").html( "left: " + position.left + ", top: " + position.top );

結果:

<p>Hello</p><p>left: 15, top: 15</p>

15. scrollTop([val]) 擷取匹配元素相對捲軸頂部的位移。

樣本

擷取第一段相對捲軸頂部的位移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "scrollTop:" + p.scrollTop() );

結果:

<p>Hello</p><p>scrollTop: 0</p>

設定相對捲軸頂部的位移

jQuery 代碼:

$("div.demo").scrollTop(300);

16. scrollLeft([val]) 擷取匹配元素相對捲軸左側的位移。

樣本

擷取第一段相對捲軸左側的位移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "scrollLeft:" + p.scrollLeft() );

結果:

<p>Hello</p><p>scrollLeft: 0</p>

設定相對捲軸左側的位移

jQuery 代碼:

$("div.demo").scrollLeft(300);

17. height([val|fn]) 取得匹配元素當前計算的高度值(px)。

樣本

擷取第一段的高

jQuery 代碼:

$("p").height();

把所有段落的高設為 20:

jQuery 代碼:

$("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)。

樣本

擷取第一段的寬

jQuery 代碼:

$("p").width();

把所有段落的寬設為 20:

jQuery 代碼:

$("p").width(20);

以 10 像素的幅度增加 p 元素的寬度

jQuery 代碼:

  $("button").click(function(){

    $("p").width(function(n,c){

    return c+10;

    });

  });

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

樣本

擷取第一段落內部地區高度。

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "innerHeight:" + p.innerHeight() );

結果:

<p>Hello</p><p>innerHeight: 16</p>

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

樣本

描述:

擷取第一段落內部地區寬度。

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "innerWidth:" + p.innerWidth() );

結果:

<p>Hello</p><p>innerWidth: 40</p>

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

樣本

描述:

擷取第一段落外部高度。

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight(true):" + p.outerHeight(true) );

結果:

<p>Hello</p><p>outerHeight: 35 , outerHeight(true):55</p>

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

樣本

描述:

擷取第一段落外部寬度。

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>

jQuery 代碼:

var p = $("p:first");

$("p:last").text( "outerWidth:" + p.outerWidth() + " , outerWidth(true):" + p.outerWidth(true) );

結果:

<p>Hello</p><p>outerWidth: 65 , outerWidth(true):85</p>

 

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.