這是一篇關於jQuery 的css()方法用來讀取HTML元素的css屬性的文章。下面我們就一起學習吧.
讀取元素的CSS語法語法如下:
css(“propertyname“);
比如下面代碼取得第一個<p>元素的背景顏色。
[javascript]
$("p").css("background-color");
$("p").css("background-color");
使用下面的語法來設置HTML元素的CSS屬性:
css(“propertyname“,”value“);
例如,下面代碼為所有<p>元素設置背景色為黃色。
[html]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("p").css("background-color", "yellow");
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p style="background-color: #ff0000">This is a paragraph.</p>
<p style="background-color: #00ff00">This is a paragraph.</p>
<p style="background-color: #0000ff">This is a paragraph.</p>
<p>This is a paragraph.</p>
<button>Set background-color of p</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("p").css("background-color", "yellow");
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p style="background-color: #ff0000">This is a paragraph.</p>
<p style="background-color: #00ff00">This is a paragraph.</p>
<p style="background-color: #0000ff">This is a paragraph.</p>
<p>This is a paragraph.</p>www.2cto.com
<button>Set background-color of p</button>
</body>
</html>
css()也支持同時多個CSS屬性:其語法如下:
css({“propertyname“:”value“,”propertyname“:”value“,…});
比如:
[javascript] view plaincopyprint?
$("p").css({"background-color":"yellow","font-size":"200%"});
$("p").css({"background-color":"yellow","font-size":"200%"});
讀取元素的CSS語法語法如下:
css(“propertyname“);
比如下面代碼取得第一個
元素的背景顏色。
[javascript]
$("p").css("background-color");
$("p").css("background-color");
使用下面的語法來設置HTML元素的CSS屬性:
css(“propertyname“,”value“);
例如,下面代碼為所有
元素設置背景色為黃色。
[html]
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a paragraph.
css()也支持同時多個CSS屬性:其語法如下:
css({“propertyname“:”value“,”propertyname“:”value“,…});
比如:
[javascript] view plaincopyprint?
$("p").css({"background-color":"yellow","font-size":"200%"});
$("p").css({"background-color":"yellow","font-size":"200%"});
以上就是我們為大家准備讀寫HTML元素的css 屬性的相關內容,希望對大家可以有所幫助。