jquery中css操作
2015-12-04 14:14
459 查看
(1)标签class操作
1.为标签添加一个class
2.为标签添加多个class
3.为标签删除class
4.对标签的class属性进行增加/删除切换操作
(2)标签css属性操作
1.获得标签css属性值
2.设置标签单个css属性
3.设置标签多个css属性
1.为标签添加一个class
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1.10.2/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>Heading 1</h1> <h2>Heading 2</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <div>This is some important text!</div> <br> <button>Add classes to elements</button> </body> </html>
2.为标签添加多个class
$("button").click(function(){ $("#div1").addClass("important blue"); });
3.为标签删除class
$("button").click(function(){ $("h1,h2,p").removeClass("blue"); });
4.对标签的class属性进行增加/删除切换操作
$("button").click(function(){ $("h1,h2,p").toggleClass("blue"); });
(2)标签css属性操作
1.获得标签css属性值
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Background color = " + $("p").css("background-color")); }); }); </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> <button>Return background-color of p</button> </body> </html>
2.设置标签单个css属性
$("p").css("background-color","yellow");
3.设置标签多个css属性
$("p").css({"background-color":"yellow","font-size":"200%"});
相关文章推荐
- jQuery1.9.1源码分析系列(十六)ajax之ajax框架
- 页面跳转
- 图片滚动JQUERY插件:jCarouselLite
- jquery等待效果示例
- jquery遍历操作
- Jquery Table 的基本操作
- 初试jQuery EasyUI
- Jquery 中 ajaxSubmit使用讲解
- RadioButtonList jquery获取ID和text
- jQuery提交Form表单的正确方式
- DrawSVG - SVG 路径动画 jQuery 插件
- 10005---jQuery 选择器
- Jquery封装ajax
- jQuery学习-选择器
- 用jQuery 写前端 关于不执行的问题
- 10004---jQuery 语法
- jQuery使用$.ajax进行异步刷新的方法(附demo下载)
- jQuery 新版本的 Dom 元素动态绑定事件
- jquery 常用的备忘
- jquery动画循环播放