Home  >  Article  >  Web Front-end  >  How to find elements without a certain attribute in jquery

How to find elements without a certain attribute in jquery

青灯夜游
青灯夜游Original
2021-11-15 18:36:522993browse

Search method: 1. Use ":not()" and "[attribute]" selectors, syntax "$("Element:not([attribute])")"; 2. Use not() Method and "[attribute]" selector, syntax "$(element).not([attribute])".

How to find elements without a certain attribute in jquery

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

In jquery, if you want to find elements containing a certain attribute, you need to use the attribute selector [attribute].

To find elements that do not contain a certain attribute, you need to combine a negative selector ":not()" or the not() method.

Grammar:

:not([attribute])
not([attribute])

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("li:not([id])").css("background-color", "#B2E0FF");
				$("li").not("[class]").css("background-color", "#ff0307");
			});
		</script>

	</head>
	<body>
		<html>
			<body>
				<div id="choose">
					Who is your favourite:
					<ul>
						<li id="li1">Goofy</li>
						<li id="li2" class="li">Mickey</li>
						<li class="li">Pluto</li>
					</ul>
				</div>
			</body>
		</html>


	</body>
</html>

How to find elements without a certain attribute in jquery

## Recommended related tutorials:

jQuery video tutorial

The above is the detailed content of How to find elements without a certain attribute 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