1. For example, if you want to get the element with id in the attribute of the page p tag
$("p[id]").css("color","red");
Get the element based on the attribute value
1.$. In jQuery, $(""), this syntax is equivalent to $(document.createElement("span")), which is a usage, when selecting elements Sometimes it is also used like this: [attribute$=value], which matches elements where the given attribute ends with a certain value. Let’s take an example to illustrate:
HTML code
<input name="newsletter" /> <input name="milkman" /> <input name="jobletter" />
jQuery code:
$("input[name$='letter']")
Result:
[ <input name="newsletter" />, <input name="jobletter" /> ]
2. !. Selector: [attribute!=value], 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]).
Example:
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" /> ]
3.* . Selector: [attribute*=value], matches the given attribute to elements containing certain values. Let’s give an example:
HTML code:
<input name="man-news" /> <input name="milkman" /> <input name="letterman2" /> <input name="newmilk" />
jQuery code:
$("input[name*='man']")
Result:
[ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ]
4.@. Matches elements containing the given attribute. Note that in jQuery 1.3, the leading @ symbol has been deprecated! If you want to be compatible with the latest version, simply remove the @ symbol
.
5.^. Selector: [attribute^=value], matches the given attribute that starts with certain values. Here is an example to illustrate:
HTML code:
<input name="newsletter" /> <input name="milkman" /> <input name="newsboy" />
jQuery code:
$("input[name^='news']")
Result:
[ <input name="newsletter" />, <input name="newsboy" /> ]
6 Get the element with the specified attribute and the set value has the specified string
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$='letter'][value$='zz']").attr("checked","true");支持多条件操作
Of course, it can also be obtained based on the id attribute or other attributes, such as $("input[id=id1]").css("color",red);
In jquery, when using $("input[name='metaId']").val(), the value of the selected radio cannot be obtained directly, but only the first value of the radio tag is obtained. This may be used by jquery xpath language is related to searching, and we usually want to get the value of the selected radio. There are several methods:
1, use $("input[name='metaId']:checked") .val() gets //name represents the name attribute name in radio
2, use $(":radio:checked").val() to get //limit the page to only one set of radio tags
The above is the detailed content of Summary of 6 methods to obtain elements in jquery. For more information, please follow other related articles on the PHP Chinese website!