jQuery CSS操作

來源:互聯網
上載者:User

標籤:

1、css() 設定或返回匹配元素的樣式屬性

(1)、返回 CSS 屬性值

   返回第一個匹配元素的 CSS 屬性值。注釋:當用於返回一個值時,不支援簡寫的 CSS 屬性(比如 "background" 和 "border")。

      文法:$(selector).css(name)

   name必需。規定 CSS 屬性的名稱。該參數可包含任何 CSS 屬性。比如 "color"。

執行個體:取得第一個段落的 color 樣式屬性的值

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    alert($("p").css("color"));  });});</script></head><body><p style="color:red">This is a paragraph.</p><button type="button">返回段落的顏色</button></body></html>


(2)、設定 CSS 屬性

   設定所有匹配元素的指定 CSS 屬性。

        文法:$(selector).css(name,value)

   name必需。規定 CSS 屬性的名稱。該參數可包含任何 CSS 屬性,比如 "color"。

   value可選。規定 CSS 屬性的值。該參數可包含任何 CSS 屬性值,比如 "red"。如果設定了Null 字元串值,則從元素中刪除指定屬性。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").css("color","red");  });});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button type="button">改變段落的顏色</button></body></html>



(3)、使用函數來設定 CSS 屬性

   設定所有匹配的元素中樣式屬性的值。此函數返回要設定的屬性值。接受兩個參數,index 為元素在對象集合中的索引位置,value 是原先的屬性值。

      文法:$(selector).css(name,function(index,value))

    name必需。規定 CSS 屬性的名稱。該參數可包含任何 CSS 屬性,比如 "color"。

    function(index,value) 規定返回 CSS 屬性新值的函數。

    index - 可選。接受選取器的 index 位置

    oldvalue - 可選。接受 CSS 屬性的當前值。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("div").click(function() {    $(this).css(      "width", function(index, value) {return parseFloat(value) *3;}    );  });});</script><style>div {width:100px; height:50px; background-color:red;}</style></head><body><div>請點擊這裡</div></body></html>


(4)、設定多個 CSS 屬性/值對

      文法:$(selector).css({property:value, property:value, ...})

  把“名/值對”對象設定為所有匹配元素的樣式屬性。這是一種在所有匹配的元素上設定大量樣式屬性的最佳方式。

  {property:value} 必需。規定要設定為樣式屬性的“名稱/值對”對象。該參數可包含若干對 CSS 屬性名稱/值。比如 {"color":"red","font-weight":"bold"}

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").css({      "color":"white",      "background-color":"#98bf21",      "font-family":"Arial",      "font-size":"20px",      "padding":"5px",      "width":"300px"    });  });});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button type="button">改變段落的樣式</button></body></html>


2、offset()返回第一個匹配元素相對於文檔的位置

   offset() 方法返回或設定匹配元素相對於文檔的位移(位置)。

(1)、返回位移座標

   返回第一個匹配元素的位移座標。該方法返回的對象包含兩個整型屬性:top 和 left,以像素計。此方法只對可見元素有效。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    x=$("p").offset();    $("#span1").text(x.left);    $("#span2").text(x.top);  });});</script></head><body><p>本段落的位移是 <span id="span1">unknown</span> left 和 <span id="span2">unknown</span> top。</p><button>獲得 offset</button></body></html>

(2)、設定位移座標

  設定所有匹配元素的位移座標。

      文法:$(selector).offset(value)

  value必需。規定以像素計的 top 和 left 座標。可能的值:值對,比如 {top:100,left:0}、帶有 top 和 left 屬性的對象

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").offset({top:100,left:10});  });});</script></head><body><p>This is a paragraph.</p><button>設定新的位移</button></body></html>


(3)、使用函數來設定位移座標

   使用函數來設定所有匹配元素的位移座標。

       文法:$(selector).offset(function(index,oldoffset))

   function(index,oldoffset) 規定返回被選元素新位移座標的函數。

   index - 可選。接受選取器的 index 位置

   oldvalue - 可選。接受選取器的當前座標。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").offset(function(n,c){    newPos=new Object();        newPos.left=c.left+100;        newPos.top=c.top+100;        return newPos;    });  });});</script></head><body><p>這是一個段落。</p><button>設定 p 元素的 offset 座標</button></body></html>

3、position()返回第一個匹配元素相對於父元素的位置

   position() 方法返回匹配元素相對於父元素的位置(位移)。該方法返回的對象包含兩個整型屬性:top 和 left,以像素計。此方法只對可見元素有效。

      文法:$(selector).position()

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    x=$("p").position();    alert("Left position: " + x.left + " Top position: " + x.top);  });});</script></head><body><p>This is a paragraph.</p><button>獲得 p 元素的位置座標</button></body></html>

4、width() 設定或返回匹配元素的寬度

(1)返回寬度

   返回第一個匹配元素的寬度。如果不為該方法設定參數,則返回以像素計的匹配元素的寬度。

          文法:$(selector).width()

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $(".btn1").click(function(){    $("span").text($("p").width());  });});</script></head><body><p>本段落的寬度是 <span>unknown</span> px。</p><button class="btn1">獲得寬度</button></body></html>


(2)、設定寬度

   設定所有匹配元素的寬度。

        文法:$(selector).width(length)

   length可選。規定元素的寬度。如果沒有規定長度單位,則使用預設的 px 單位。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $(".btn1").click(function(){    $("p").width(200);  });});</script></head><body><p style="background-color:yellow">This is a paragraph.</p><button class="btn1">改變寬度</button></body></html>


 

(3)、使用函數來設定寬度

   使用函數來設定所有匹配元素的寬度。

       文法:$(selector).width(function(index,oldwidth))

   function(index,oldwidth) 規定返回被選元素新寬度的函數。

   index - 可選。接受選取器的 index 位置

   oldvalue - 可選。接受選取器的當前值。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").width(function(n,c){    return c-50;    });  });});</script></head><body><p style="background-color:yellow">這是一個段落。</p><button>以 50 像素的幅度減少 p 元素的寬度</button></body></html>

5、height() 設定或返回匹配元素的高度

(1)、返回高度

   返回第一個匹配元素的高度。如果不為該方法設定參數,則返回以像素計的匹配元素的高度。

       文法:$(selector).height()

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $(".btn1").click(function(){    $("span").text($("p").height());  });});</script></head><body><p>本段落的高度是 <span>unknown</span> px。</p><button class="btn1">獲得高度</button></body></html>


(2)、設定高度

   設定所有匹配元素的高度。

       文法:$(selector).height(length)

   length可選。規定元素的高度。如果沒有規定長度單位,則使用預設的 px 單位。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $(".btn1").click(function(){    $("p").height(50);  });});</script></head><body><p style="background-color:yellow">This is a paragraph.</p><button class="btn1">改變高度</button></body></html>


 

(3)、使用函數來設定高度

   使用函數來設定所有匹配元素的高度。

       文法:$(selector).height(function(index,oldheight))

   function(index,oldheight) 規定返回被選元素新高度的函數。

   index - 可選。接受選取器的 index 位置

   oldvalue - 可選。接受選取器的當前值。

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("p").height(function(n,c){    return c+10;    });  });});</script></head><body><p style="background-color:yellow">這是一個段落.</p><button>以 10 像素的幅度增加 p 元素的高度</button></body></html>


 

 

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.