This document describes six methods to obtain elements based on attributes or attribute values. For more information about how to obtain elements based on attributes, see
1. For example, to obtain the element with the property id in the p tag of the page
The Code is as follows:
$ ("P [id]" ).css ("color", "red ");
Get element based on Attribute Value
1. $. In jQuery $ (""), this syntax is equivalent to $ (document. createElement ("span"), which is a usage. when selecting an element, [attribute $ = value], matches the given attribute with elements ending with some values. The following is an example:
HTML code
The Code is as follows:
JQuery code:
The Code is as follows:
$ ("Input [name $ = 'Letter ']")
Result:
[ , ]
2 .!. Selector: [attribute! = Value]: matches all elements that do not contain the specified attribute or that are not equal to the specified value. This selector is equivalent to not ([attr = value]).
Example:
HTML code
The Code is as follows:
JQuery code:
The Code is as follows:
$ ("Input [name! = 'Newsletter '] "). attr (" checked ", true );
Result:
[ ]
3 .*. Selector: [attribute * = value]. matching a given attribute is an element that contains some values. For example:
HTML code:
The Code is as follows:
JQuery code:
The Code is as follows:
$ ("Input [name * = 'man']")
Result:
[ , , ]
4 .@. Matches the element that contains the given attribute. Note: In jQuery 1.3, the leading @ symbol has been abolished! To be compatible with the latest version, simply remove the @ symbol
Yes.
5. ^. Selector: [attribute ^ = value]. matching a given attribute is an element starting with some values. The following is an example:
HTML code:
The Code is as follows:
JQuery code:
The Code is as follows:
$ ("Input [name ^ = 'News']")
Result:
[ , ]
6. Obtain the elements of a specified attribute with a specified string in the set value.
HTML code:
The Code is as follows:
JQuery code:
The Code is as follows:
$ ("Input [name $ = 'Letter '] [value $ = 'zz']"). attr ("checked", "true"); supports multi-condition operations
Of course, you can also obtain it based on the id attribute or other attributes, such as $ ("input [id = id1]" ).css ("color", red );
In jquery, $ ("input [name = 'metaid ']") is used. val () cannot directly obtain the selected radio value, but only obtain the first value of the radio tag. This may be related to jquery's search using the xpath language, we usually want to obtain the selected radio value. 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 only one set of radio tags on the page.