Jquery css函数实现动态操纵DOM节点的样式
JQuery中css函数可以给DOM节点设置效果,CSS函数一般有以下几种用法:
一、判断一个对象是否隐藏:
$("#id").css("display")=="none" ;
二、在所有匹配的元素中,设置一个样式属性的值:
$("div").css("color","#FF0000");
三、把一个“名/值对”对象设置为所有匹配元素的样式属性。 这是一种在所有匹配的 元素上设置大量样式属性的最佳方式:
$("div").css({ color: "#ff0000", background: "blue" });
如果属性名包含 "-"的话,必须使用引号:
$("div").css({ "margin-left": "10px", "background-color": "blue" });
下面是自己写的一些个演示代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta name="author" content="LuisZhang"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title><script type="text/javascript" src="js/jquery-1.4.2.min.js"></script><script type="text/javascript">$(function() {// $("div").click(function() { alert($(this).next("div").text()); });// $("div").click(function() { alert($(this).nextAll("div").text()); });// $("div").click(function() { $.each($(this).nextAll("div"), function() { $(this).css("background", "red") }); });$("p").click(function() { $.each($(this).nextAll("p"), function() { $(this).css("background", "#abccdd") }); });$("div").click(function() { $.each($(this).next("div"), function() { $(this).css({ "margin-left": "10px", color: "#abccdd", background: "blue" }) }); });$("#fristDiv").click(function() { $.each($(this), function() { $(this).css({ "margin-left": "10px", color: "#abccdd", background: "blue" }) }); });$("#lastDiv").click(function() { $.each($(this), function() { $(this).css({ "margin-left": "10px", color: "#abccdd", background: "blue" }) }); });});</script></head><body><div id="fristDiv">aa</div><div>bb</div><div>cc</div><div>dd</div><p>p1</p><p>p2</p><p>p3</p><p>p4</p><div id="lastDiv">ee</div></body></html>