jQuery使用手冊之CSS操作(4)

來源:互聯網
上載者:User

Jquery對css的操作相當方便,能很方便我們去通過js修改css。傳統javascript對css的操作相當繁瑣,比如

<div id="a"
style="background:blue">css</div>

取它的background文法是:

document.getElementById("a").style.background

而jQuery對css更方便的操作:

$("#a").background();$("#a").background(“red”)

說明如下
$("#a")得到jQuery對象[
<div id="a" … /div>
]
$("#a").background()將取出該對象的background樣式。
$("#a").background(“red”)將該對象的background樣式設為redjQuery提供了以下方法,來操作css:

  • background ()
  • background (val)
  • color()
  • color(val)
  • css(name)
  • css(prop)
  • css(key, value)
  • float()
  • float(val)
  • height()
  • height(val)
  • width()
  • width(val)  
  • left()
  • left(val)
  • overflow()
  • overflow(val)
  • position()
  • position(val)
  • top()
  • top(val)

這裡需要講解一下css(name);css(prop);css(key,
value),其他的看名字都知道什麼作用了!

<div id="a" style="background:blue;
color:red">css</div><P
id="b">test</P>

css(name):擷取樣式名為name的樣式

$("#a").css("color"):將得到樣式中color值red,("#a").css("background
")將得到blue

css(prop):prop是一個hash對象,用於設定大量的css樣式

$("#b").css({ color: "red", background:
"blue" });

最終效果是

<p id="b" style="background:blue;
color:red">test</p>

{ color: "red", background: "blue"
},hash對象,color為key,"red"為value,

css(key, value)  用於設定一個單獨得css樣式

$("#b").css("color","red");

最終效果是

<p id="b"
style="color:red">test</p>

聯繫我們

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