Home  >  Article  >  Web Front-end  >  How to remove element attributes in jquery

How to remove element attributes in jquery

青灯夜游
青灯夜游Original
2022-02-28 14:30:217871browse

Jquery method to remove element attributes: 1. Use removeAttr(), the syntax "$(selector).removeAttr("Attribute name to be removed")"; 2. Use attr(), the syntax " $(selector).attr("Attribute name to be removed","")".

How to remove element attributes in jquery

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

jquery removes element attributes

Method 1: Use removeAttr()

## The #removeAttr() method removes attributes from selected elements.

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("button").click(function() {
					$("p").removeAttr("style");
				});
			});
		</script>
	</head>

	<body>
		<h1>这是一个标题</h1>
		<p style="font-size:120%;color:red">这是一个段落。</p>
		<p>这是另一个段落。</p>
		<button>删除所有 p 元素的 style 属性</button>
	</body>
</html>

How to remove element attributes in jquery

Method 2: Use the attr()

attr() method to set or return the attribute value of the selected element.

When the attr() method is used to set the attribute value of the selected element to null, the specified attribute can also be invalidated.

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("button").click(function() {
					$("p").attr("style","");
				});
			});
		</script>
	</head>

	<body>
		<h1>这是一个标题</h1>
		<p style="font-size:120%;color:red">这是一个段落。</p>
		<p>这是另一个段落。</p>
		<button>删除所有 p 元素的 style 属性</button>
	</body>
</html>

How to remove element attributes in jquery

[Recommended learning:

jQuery video tutorial, web front-end development video]

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

Statement:
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