jquery怎么去除class属性

首页 编程分享 JQUERY丨JS丨VUE 正文

青灯夜游 转载 编程分享 2021-11-25 23:49:03

简介 方法:1、用attr()将class属性的值设为空,语法“$(selector).attr("class","")”;2、用removeAttr()移除class属性,语法“$(selector).removeAttr("class")”。


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

jquery去除class属性有两种方法:

  • 使用attr()将元素中class属性的值设为空

    语法:$(selector).attr("class","")

  • 使用removeAttr()移除元素中的class属性

    语法:$(selector).removeAttr("class")

示例:

<!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() {
					$(".box1").attr("class", "");
					$(".box2").removeAttr("class");
				});
			});
		</script>
		<style>
			div {
				border: 1px solid red;
				margin: 10px;
			}

			.box1 {
				background-color: IdFFC0CB;
			}

			.box2 {
				background-color: green;
				color: white;
			}
		</style>
	</head>
	<body>

		<div class="box1">测试文本</div>
		<div class="box2">测试文本</div>
		<br>
		<button>去掉class属性</button>

	</body>
</html>

相关视频教程推荐:

以上就是jquery怎么去除class属性的详细内容,更多请关注php中文网其它相关文章!

转载链接:http://www.php.cn//js-tutorial-484797.html


Tags:


本篇评论 —— 揽流光,涤眉霜,清露烈酒一口话苍茫。


    声明:参照站内规则,不文明言论将会删除,谢谢合作。


      最新评论




ABOUT ME

Blogger:袅袅牧童 | Arkin

Ido:PHP攻城狮

WeChat:nnmutong

Email:nnmutong@icloud.com

标签云