Obtain elements based on attributes
1. For example, you want to get an element with id in the page p tag
$("p[id]").css("color","red");
Obtain elements based on attribute values 1.$。 In jQuery, $("<span>"), this syntax is equivalent to $(document.createElement("span")), which is a usage that is used when selecting an element: [attribute$=value], matching a given attribute is an element ending in some value. Here is an example: HTML code Copy code The code is as follows:
<input name="newsletter" /> <input name="milkman" /> <input name="jobletter" />
jQuery code:
- $("input[name$='letter']")
Copy code
Outcome: [ <input name="newsletter" />, <input name="jobletter" /> ]
2.!。 Selector: [attribute!=value], matches all elements that do not contain a specified attribute, or whose attribute does not equal a specific value, this selector is equivalent to :not([attr=value]). Here are some examples:
<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);
Copy code
Outcome: [ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]
3.*。 Selector: [attribute*=value], matching a given attribute is an element containing certain values. Here's an example: HTML code:
<input name="man-news" /> <input name="milkman" /> <input name="letterman2" /> <input name="newmilk" />
jQuery code:
Outcome: [ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ]
4.@。 Match elements that contain a given attribute. Note that in jQuery 1.3, the leading @ symbol has been abolished! If you want to be compatible with the latest version, you just need to simply remove the @ symbol Yes.
5.^。 Selector: [attribute^=value], matching a given attribute is an element that starts with some value, here is an example: HTML code:
<input name="newsletter" /> <input name="milkman" /> <input name="newsboy" />
jQuery code:
Outcome: [ <input name="newsletter" />, <input name="newsboy" /> ]
6 Obtain an element with a specified attribute and a specified string in the setting HTML code:
- <p><input type="checkbox" name="newsletter" value="Hot Fuzz"/> </p><p><input type="checkbox" name="newsletter" value="Cold Fusion" /> </p><p><input type="checkbox" name="accept" value="Evil Plans" /> </p>
Copy code
jQuery code:
- $("input[name$='letter'][value$='zz']").attr("checked","true");支持多条件操作
Copy code
Of course, it can also be obtained based on id attributes or other attributes, such as $("input[id=id1]").css("color",red); In jquery, when using $("input[name='metaId']").val() cannot directly obtain the value of the selected radio, but only the first value of the radio tag, this may be related to jquery using the xpath language for lookup, and we usually want to get the value of the selected radio, there are several methods: 1. Use $("input[name='metaId']:checked").val() to get //name to represent the name attribute name in radio 2. Use $(":radio:checked").val() to get // Restrict the page to only one set of radio tags
|