我是靠谱客的博主 寒冷绿草,这篇文章主要介绍jquery怎么替换类样式,现在分享给大家,希望可以做个参考。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

jquery替换类样式

方法1:使用removeClass()和addClass()方法

使用removeClass()方法移除旧的类样式,使用addClass()方法添加新的类样式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").removeClass("old-class").addClass("new-class") ; }); }); </script> <style type="text/css"> .old-class{ border: 1px solid red; background-color: #FFC0CB; } .new-class{ border: 2px solid green; background-color: #55aa7f; color: white; } </style> </head> <body> <p class="old-class">hello,测试文字</p> <button>替换类样式</button> </body> </html>
登录后复制

1.gif

方法2:使用attr()

使用attr()方法修改class属性,直接替换为新的class类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").attr("class","new-class"); }); }); </script> <style type="text/css"> .old-class{ border: 1px solid red; background-color: #FFC0CB; } .new-class{ border: 2px solid green; background-color: #55aa7f; color: white; } </style> </head> <body> <p class="old-class">hello,测试文字</p> <button>替换类样式</button> </body> </html>
登录后复制

2.gif

相关视频教程推荐:jQuery教程(视频)

以上就是jquery怎么替换类样式的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是寒冷绿草最近收集整理的关于jquery怎么替换类样式的全部内容,更多相关jquery怎么替换类样式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(135)

评论列表共有 0 条评论

立即
投稿
返回
顶部