您好,欢迎访问一九零五行业门户网

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 });
下面是自己写的一些个演示代码。
[html] <!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>
其它类似信息

推荐信息