jQuery函数学习之五(css部分)

也就是对dom元素中的样式进行操作

函数:css(name)
功能:获取匹配元素指定属性名的值
返回:string
参数:要访问的样式属性名
例子:
Retrieves the color style of the first paragraph

jQuery Code
$("p").css("color");
Before
<style="color:red;">Test Paragraph.</p>
Result:
"red"

Retrieves the font-weight style of the first paragraph.

jQuery Code
$("p").css("font-weight");
Before
<style="font-weight: bold;">Test Paragraph.</p>
Result:
"bold"
函数:css(properties),css(key, value)
功能:设定一组样式,设定一个样式
返回:jQuery对象
参数:属性数组对象,属性名/值
例子:
Sets color and background styles to all p elements.

jQuery Code
$("p").css({ color: "red", background: "blue" });
Before
<p>Test Paragraph.</p>
Result:
<style="color:red; background:blue;">Test Paragraph.</p>

Changes the color of all paragraphs to red

jQuery Code
$("p").css("color","red");
Before
<p>Test Paragraph.</p>
Result:
<style="color:red;">Test Paragraph.</p>

Changes the left of all paragraphs to "30px"

jQuery Code
$("p").css("left",30);
Before
<p>Test Paragraph.</p>
Result:
<style="left:30px;">Test Paragraph.</p>
函数:height(),height(val)
功能:获取第一个匹配元素的高度,height(val)设定所有匹配元素的高度
返回:string
例子:
jQuery Code
$("p").height();
Before
<p>This is just a test.</p>
Result:
300

jQuery Code
$("p").height(20);
Before
<p>This is just a test.</p>
Result:
<style="height:20px;">This is just a test.</p

jQuery Code
$("p").height("20em");
Before
<p
>This is just a test.</p>
Result:
<style="height:20em;">This is just a test.</p>
函数:width(),width(val)
功能:获取第一个元素的宽度,width(val)设定所有匹配元素的宽度
例子:和上面差不多,就不列举了。
原文地址:https://www.cnblogs.com/jackhuclan/p/1269621.html