jQuery实现获取及设置CSS样式操作详解
2018-09-05 10:19
1006 查看
本文实例讲述了jQuery实现获取及设置CSS样式操作。分享给大家供大家参考,具体如下:
- addClass():向被选元素添加一个或多个类
- removeClass():从被选元素删除一个或多个类
- toggleClass():对被选元素进行添加/删除类的切换操作
- css():设置或返回被选元素的一个或多个样式属性
样式表实例:
.important{ font-weight:bold; font-size:xx-large; } .blue{ color:blue; }
addClass()
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").addClass("blue"); $("div").addClass("important"); }); }); </script> <style type="text/css">.important{ font-weight:bold; font-size:xx-large; } .blue{ color:blue; }</style> </head> <body> <h1>she is gone</h1> <h2>no say anymore</h2> <p>but i miss you</p> <div>can you come back</div> </br> <button>addClass</button> </body> </html>
运行效果:
可以在
addClass()方法中规定多个类:
$("button").click(function(){ $("#div1").addClass("important blue"); });
removeClass()
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").removeClass("blue"); }); }); </script> <style type="text/css"> .important{ font-weight:bold; font-size:xx-large; } .blue{ color:blue; } </style> </head> <body> <h1 class="blue">is she ?</h1> <h2 class="blue">i do not know</h2> <p class="blue">queit</p> <br> <button>removeClass</button> </body> </html>
运行结果:
toggleClass()
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").toggleClass("blue"); }); }); </script> <style type="text/css"> .blue{ color:blue; } </style> </head> <body> <h1>who are you</h1> <h2> i five thank you</h2> <p>goodbye</p> <button>toggleClass</button> </body> </html>
运行结果:
css()
返回CSS属性
css("propertyname")
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Background color = " + $("#p2").css("background-color")); }); }); </script> </head> <body> <h2>我心在南朝</h2> <p id=p1 style="background-color:#ff0000">田野上</p> <p id=p2 style="background-color:#00ff00">红花盛开</p> <p id=p3 style="background-color:#0000ff">玲珑剔透</p> <button>css()</button> </body> </html>
运行结果:
设置CSS属性
css("propertyname","value");
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").css("background-color","yellow"); }); }); </script> </head> <body> <h2>我心在南朝</h2> <p style="background-color:#ff0000">田野上</p> <p style="background-color:#00ff00">红彤彤的花儿</p> <p style="background-color:#0000ff">玲珑剔透</p> <p>我心在南朝</p> <button>设置CSS属性</button> </body> </html>
运行结果:
设置多个CSS属性
$("p").css({"background-color":"yellow","font-size":"200%"});
jQuery――尺寸
- width():设置或返回元素的宽度(不包括内边距、边框或外边距)
- height():设置或返回元素的高度(不包括内边距、边框或外边距)
- innerWidth()方法返回元素的宽度(包括内边距)
- innerHeight()方法返回元素的高度(包括内边距)
- outerWidth()方法返回元素的宽度(包括内边距和边框)
- outerHeight()方法返回元素的高度(包括内边距和边框)
- outerWidth(true)方法返回元素的宽度(包括内边距、边框、外边距)
- outerHeight(true)方法返回元素的高度(包括内边距、边框、外边距)
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var txt=""; txt+="Width of div: " + $("#div1").width() + "</br>"; txt+="Height of div: " + $("#div1").height() + "</br>"; txt+="innerWidth of div: " + $("#div1").innerWidth() + "</br>"; txt+="innerHeight of div: " + $("#div1").innerHeight() + "</br>" txt+="Outer width: " + $("#div1").outerWidth() + "</br>" txt+="Outter height: " + $("#div1").outerHeight() + "</br>"; txt+="Outer width: " + $("#div1").outerWidth(true) + "</br>" txt+="Outter height: " + $("#div1").outerHeight(true); $("#div1").html(txt); }); }); </script> </head> <body> <div id="div1" style="height:150px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div> <br> <button>显示div尺寸</button> </body> </html>
运行结果:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具http://tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。
更多关于jQuery相关内容还可查看本站专题:《jQuery切换特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常用插件及用法总结》、《jQuery拖拽特效与技巧总结》、《jQuery表格(table)操作技巧汇总》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》
希望本文所述对大家jQuery程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- JQuery获取与设置HTML元素的内容或文本的实现代码
- jQuery 获取和设置select下拉框的值实现代码
- jQuery 获取和设置select下拉框的值实现代码
- JQuery获取与设置HTML元素的内容或文本的实现代码
- JQuery获取与设置HTML元素的内容或文本的实现代码
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- JQuery获取与设置HTML元素的内容或文本的实现代码
- jquery实现input框获取焦点的简单实例
- 2015-07-23 JQuery第三课(JQuery元素:获取、添加、删除、设置、绑定、显示隐藏、回调函数、动画)
- jquery的ajax和getJson跨域获取json数据的实现方法
- jquery遍历table的tr获取td的值实现方法
- jquery获取和设置radio,check,select选项
- jQuery元素属性操作实例(设置、获取及删除元素属性)
- jQuery获取及设置表单input各种类型值的方法小结
- jQuery的简单应用-实现文本框获取焦点时改变样式
- jquery multiselect如何实现下拉框多选并获取多个选项的值
- Query常用DIV操作获取和设置长度宽度的实现方法
- JQuery实现下拉框的选择 与当CheckBox为服务器控件时如何获取值的操作,实现全选与删除
- Jquery实现全选和反选并获取选中行
- jquery实现在页面加载完毕后获取图片高度或宽度