JQUERY學習之:CSS操作

來源:互聯網
上載者:User

 

     
傳統
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
><

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.