JQUERY学习之:CSS操作

来源:互联网 发布:林丹谢杏芳感情知乎 编辑:程序博客网 时间:2024/06/15 15:25

 

      传统javascriptcss的操作相当繁琐,比如<divid="a" style="background:blue">css</div>取它的background语法是 document.getElementById("a").style.background,而jQuerycss更方便的操作,$("#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")
将得到样式中colorred("#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对象,colorkey"red"value
css(key, value)  用于设置一个单独得css样式
$("#b").css("color","red");最终效果是<p id="b"style="color:red">test</p>                   

原创粉丝点击