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
Attribute name
value StringV1.0
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" />
jQuery Code:
$("input[name!='newsletter']").attr("checked", true);
Result:
[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]
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]")
Parameter list:
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!='second']").css("color","blue"); }) }) </script> </head> <body> <ul> <li id="first">html专区</li> <li id="second">Jquery专区</li> </ul> <button>点击查看效果</button> </body> </html>
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>
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!