Home > Web Front-end > JS Tutorial > Instructions for using jQuery selector [attribute!=value]

Instructions for using jQuery selector [attribute!=value]

黄舟
Release: 2017-06-23 13:21:19
Original
1305 people have browsed it

Overview

Matches all elements that do not contain the specified attribute, or the attribute is not equal to a specific value.

This selector is equivalent to:not([attr=value])
To match elements that contain a specific attribute but are not equal to a specific value, use [attr]:not([attr=value ])

Parameters

attributeStringV1.0
Copy after login

Attribute name

value StringV1.0
Copy after login

Attribute value. Quotes are optional in most cases. But it is used to avoid conflicts when the attribute value contains "]".

Example

Description:

Find all input elements whose name attribute is not newsletter

HTML code:

<input type="checkbox" name="newsletter" value="Hot Fuzz" /> 
<input type="checkbox" name="newsletter" value="Cold Fusion" /> 
<input type="checkbox" name="accept" value="Evil Plans" />
Copy after login

jQuery Code:

$("input[name!=&#39;newsletter&#39;]").attr("checked", true);
Copy after login

Result:

[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]
Copy after login

This selector matches all elements that do not contain the specified attribute, or whose attribute is not equal to a specific value.
This selector is equivalent to: not[([attr=value]).
To match elements that contain a specific attribute but are not equal to a specific value, use [attr]:not([attr=value])

Syntax structure:

$("[attribute!=value]")
Copy after login

Parameter list:

Instructions for using jQuery selector [attribute!=value]

Example code:
Example 1:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title></title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("button").click(function(){ 
    $("li[id!=&#39;second&#39;]").css("color","blue"); 
  })
})
</script>
</head>
<body>
<ul>
  <li id="first">html专区</li>
  <li id="second">Jquery专区</li>
</ul>
<button>点击查看效果</button>
</body>
</html>
Copy after login

The above code can change the id attribute value in the li element that is not equal to second or does not have an id attribute value. The text color in the li element is set to blue.
Example 2:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title></title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("button").click(function(){ 
    $("li[id!=sec[ond]").css("color","blue"); 
  }); 
}); 
</script>
</head>
<body>
<ul>
  <li id="first">html专区</li>
  <li id="sec[ond">Jquery专区</li>
</ul>

<button>点击查看效果</button>
</body>
</html>
Copy after login

From the above code, we can see how when the code contains "[" or "]", it must have quotation marks. , otherwise it will cause matching errors.


The above is the detailed content of Instructions for using jQuery selector [attribute!=value]. For more information, please follow other related articles on the PHP Chinese website!

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