您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 百色分类信息网,免费分类信息发布

jquery怎么添加类和移除类

2024/4/17 13:56:26发布13次查看
jquery中可用addclass()和removeclass()来添加类和移除类。addclass()向被选元素添加一个或多个类,语法“$(selector).addclass(类名)”,如需添加多个类,就使用空格分隔类名。removeclass()从被选元素移除一个或多个类,语法“$(selector).removeclass(类名)”;参数可以省略,此时就会清空所有类。
本教程操作环境:windows7系统、jquery3.6.1版本、dell g3电脑。
jquery添加类和移除类的方法
方法描述
addclass() 向匹配的元素添加指定的类名。
removeclass() 从所有匹配的元素中删除全部或者指定的类。
jquery addclass()添加类
addclass() 方法向被选元素添加一个或多个类。
该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。
提示:如需添加多个类,请使用空格分隔类名。
语法:
$(selector).addclass(class)
参数描述
class 必需。规定一个或多个 class 名称。
示例:向第一个 p 元素添加一个类
<!doctype html><html> <head> <script src="js/jquery-3.6.1.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>this is a heading</h1> <p>this is a paragraph.</p> <p>this is another paragraph.</p> <button>向第一个 p 元素添加一个类</button> </body></html>
jquery removeclass()移除类
removeclass() 方法从被选元素移除一个或多个类。
注释:如果没有规定参数,则该方法将从被选元素中删除所有类。
语法:
$(selector).removeclass(class)
参数描述
class 可选。规定要移除的 class 的名称。
如需移除若干类,请使用空格来分隔类名。
如果不设置该参数,则会移除所有类。
示例:移除所有 <p> 的 "intro" 类
<!doctype html><html> <head> <script src="js/jquery-3.6.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { $("p").removeclass("intro"); }); }); </script> <style type="text/css"> .intro { font-size: 120%; color: red; } </style> </head> <body> <h1 id="h1">this is a heading</h1> <p class="intro">this is a paragraph.</p> <p>this is another paragraph.</p> <button>从第一个段落中删除类</button> </body></html>
【推荐学习:jquery视频教程、web前端视频】
以上就是jquery怎么添加类和移除类的详细内容。
百色分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录