Get elements based on attributes or attribute values in JQuery (6 methods)

Source: Internet
Author: User

Get elements based on Attributes

1. For example, to obtain the element with the property id in the p tag of the pageCopy codeThe Code is as follows: $ ("p [id]" ).css ("color", "red ");

Get element based on Attribute Value
1. $. In jQuery $ ("<span>"), 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 codeCopy codeThe Code is as follows: <input name = "newsletter"/>
<Input name = "milkman"/>
<Input name = "jobletter"/>

JQuery code:Copy codeThe Code is as follows: $ ("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 that are not equal to the specified value. This selector is equivalent to not ([attr = value]).
Example:
HTML codeCopy codeThe Code is as follows: <input type = "checkbox" name = "newsletter" value = "Hot Fuzz"/>
<Input type = "checkbox" name = "newsletter" value = "Cold Fusion"/>
<Input type = "checkbox" name = "accept" value = "edevil Plans"/>

JQuery code:Copy codeThe Code is as follows: $ ("input [name! = 'Newsletter '] "). attr (" checked ", true );

Result:
[<Input type = "checkbox" name = "accept" value = "edevil Plans" checked = "true"/>]

3 .*. Selector: [attribute * = value]. matching a given attribute is an element that contains some values. For example:
HTML code:Copy codeThe Code is as follows: <input name = "man-news"/>
<Input name = "milkman"/>
<Input name = "letterman2"/>
<Input name = "newmilk"/>

JQuery code:Copy codeThe Code is as follows: $ ("input [name * = 'man']")

Result:
[<Input name = "man-news"/>, <input name = "milkman"/>, <input name = "letterman2"/>]

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:Copy codeThe Code is as follows: <input name = "newsletter"/>
<Input name = "milkman"/>
<Input name = "newsboy"/>

JQuery code:Copy codeThe Code is as follows: $ ("input [name ^ = 'News']")

Result:
[<Input name = "newsletter"/>, <input name = "newsboy"/>]

6. Obtain the elements of a specified attribute with a specified string in the set value.
HTML code:Copy codeThe Code is as follows: <input type = "checkbox" name = "newsletter" value = "Hot Fuzz"/>
<Input type = "checkbox" name = "newsletter" value = "Cold Fusion"/>
<Input type = "checkbox" name = "accept" value = "edevil Plans"/>

JQuery code:Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.