jquery如何移除某个class

本篇内容主要讲解“jquery如何移除某个class”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“jquery如何移除某个class”吧!

移除方法:1、用removeClass()从被选元素移除指定类,语法“元素对象.removeClass(class名称)”;2、用removeAttr()从元素中移除class属性,语法“元素对象.removeAttr("class")”。

jquery如何移除某个class  jquery 第1张

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

jquery移除某个class

方法1:使用removeClass()

removeClass() 方法从被选元素移除一个或多个类。

$(selector).removeClass(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(){
				  $("p:first").removeClass("intro");
				});
			});
			</script>
		<style type="text/css">
			.intro {
				font-size: 120%;
				color: red;
			}
		</style>
	</head>
	<body>

		<h2>这是一个段落标题</h2>
		<p class="intro">这是一个段落</p>
		<p> 这是另外一个段落</p>
		<button>移除p元素的 "intro" 类</button>

	</body>
</html>

jquery如何移除某个class  jquery 第2张

方法2:使用removeAttr()

removeAttr() 方法从被选元素中移除属性。

只需要使用该方法从被选元素中移除class属性即可移除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(){
				  $("p:first").removeAttr("class");
				});
			});
			</script>
		<style type="text/css">
			.intro {
				font-size: 120%;
				color: red;
			}
		</style>
	</head>
	<body>

		<h2>这是一个段落标题</h2>
		<p class="intro">这是一个段落</p>
		<p> 这是另外一个段落</p>
		<button>移除p元素的 class</button>

	</body>
</html>

jquery如何移除某个class  jquery 第3张

到此,相信大家对“jquery如何移除某个class”有了更深的了解,不妨来实际操作一番吧!这里是蜗牛博客网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接