Home > Web Front-end > JS Tutorial > How to delete styles in jquery

How to delete styles in jquery

青灯夜游
Release: 2021-11-23 19:16:24
Original
5285 people have browsed it

Deletion method: 1. Use the removeAttr() method, the syntax is "$(element).removeAttr("Attribute name")", the attribute name can be id, class or style; 2. Use the removeClass() method , syntax "$(element).removeClass("class name")".

How to delete styles in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery method of removing styles

Method 1: Use the removeAttr() method

removeAttr () method removes attributes from selected elements.

<!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() {
					$("h1").removeAttr("id");
					$("p").removeAttr("style");
					$("p").removeAttr("class");
				});
			});
		</script>
		<style>
			#h1{
				color: red;
			}
			.box{
				color: #FFC0CB;
			}
		</style>
	</head>

	<body>
		<h1 id="h1">这是一个标题</h1>
		<p style="font-size:120%;color:red">这是一个段落。</p>
		<p class="box">这是另一个段落。</p>
		<button>删除样式</button>
	</body>
</html>
Copy after login

Rendering:

How to delete styles in jquery

Method 2: Use the removeClass() method

removeClass( ) method removes one or more classes from the selected elements.

Note: If no parameters are specified, this method will remove all classes from the selected elements.

<!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();
					$("div").removeClass("intro");
				});
			});
		</script>
		<style>
			.box1 {
				color: #FFC0CB;
			}

			.box2 {
				color: #FFC0CB;
			}

			.box3 {
				color: #FFC0CB;
			}

			.intro {
				font-size: 120%;
				color: red;
			}
		</style>
	</head>

	<body>
		<p class="box1">这是一个段落。</p>
		<p class="box2">这是一个段落。</p>
		<p class="box3">这是一个段落。</p>
		<div class="intro">这是另一个段落。</div><br>
		<button>删除样式</button>
	</body>
</html>
Copy after login

How to delete styles in jquery

Recommended related video tutorials: jQuery Tutorial (Video)

The above is the detailed content of How to delete styles in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template