脚本宝典收集整理的这篇文章主要介绍了jquery addclass()怎么用,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在jquery中,addclass()方法用于向被选元素添加一个或多个类,语法为“$(selector).addClass(“类名”)”;如需添加多个类,需要使用空格分隔类名。
本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
在jquery中,addclass()方法用于向被选元素添加一个或多个类。
该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。
提示:如需添加多个类,请使用空格分隔类名。
语法
$(selector).addClass(class)
也使用函数向被选元素添加类
$(selector).addClass(function(index,oldclass))
示例:
向第一个 p 元素添加一个类
<!DOCTYPE htML> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { $("p:First").addClass("intro"); }); }); </script> <style type="text/css"> .intro { font-Size: 120%; color: red; } </style> </head> <body> <h1>这是一个大标题</h1> <p>这是一个段落。</p> <p>这是另一个段落。</p> <button>向第一个 p 元素添加一个类</button> </body> </html>
向元素添加两个类
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { $("p:first").addClass("intro note"); }); }); </script> <style type="text/css"> .intro { font-size: 120%; color: blue; } .note { background-color: yellow; } </style> </head> <body> <h1>这是一个大标题</h1> <p>这是一个段落。</p> <p>这是另一个段落。</p> <button>向第一个 p 元素添加一个类</button> </body> </html>
【推荐学习:jQuery视频教程、web前端】
以上就是jquery addclass()怎么用的详细内容,更多请关注脚本宝典其它相关文章!
以上是脚本宝典为你收集整理的jquery addclass()怎么用全部内容,希望文章能够帮你解决jquery addclass()怎么用所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。