In jquery, the element is obtained based on the attribute or attribute value (6 ways to get the method) _jquery

Source: Internet
Author: User
To get an element from a property

1. For example, to get the element with ID in the page p tag
Copy Code code as follows:

$ ("P[id]"). CSS ("Color", "red");

Get an element from a property value
1.$. In jquery $ ("<span>"), this syntax is equivalent to $ (document.createelement ("span"), which is a usage that can be used when selecting elements: [Attribute$=value], Matching a given property is an element that ends with some value. Here is an example to illustrate:
HTML code
Copy Code code as follows:

<input name= "Newsletter"/>
<input name= "Milkman"/>
<input name= "Jobletter"/>

JQuery Code:
Copy Code code as follows:

$ ("input[name$= ' letter ']")

Results:
[<input name= "Newsletter"/&GT; <input name= "Jobletter"/>]

2.!. selector: [Attribute!=value], which matches all elements that do not contain the specified property, or the property is not equal to a particular value, which is equivalent to: not ([Attr=value]).
Examples illustrate:
HTML code
Copy Code code as follows:

<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:
Copy Code code as follows:

$ ("input[name!= ' newsletter ']"). attr ("Checked", true);

Results:
[<input type= "checkbox" Name= "Accept" value= "Evil plans" checked= "true"/>]

3.*. selector: [Attribute*=value], which matches the given property to an element that contains certain values. For example, explain:
HTML Code:
Copy Code code as follows:

<input name= "Man-news"/>
<input name= "Milkman"/>
<input name= "Letterman2"/>
<input name= "Newmilk"/>

JQuery Code:
Copy Code code as follows:

$ ("input[name*= ' Man ']")

Results:
[<input name= "Man-news"/&GT; <input name= "Milkman"/&GT; <input name= "letterman2"/>]

4.@. Matches the element that contains the given property. Note that in jquery 1.3, the leading @ symbol has been abolished! If you want to be compatible with the latest version, simply remove the @ symbol
Can

5.^. selector: [Attribute^=value], which matches a given property as an element that starts with some value, and here's an example to illustrate:
HTML Code:
Copy Code code as follows:

<input name= "Newsletter"/>
<input name= "Milkman"/>
<input name= "Newsboy"/>

JQuery Code:
Copy Code code as follows:

$ ("input[name^= ' News ']")

Results:
[<input name= "Newsletter"/&GT; <input name= "Newsboy"/>]

6 Gets the specified property and the element with the specified string in the Set value
HTML Code:
Copy Code code as follows:

<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:
Copy Code code as follows:

$ ("input[name$= ' letter '][value$= ' ZZ ']"). attr ("Checked", "true"); Support for multi-condition operations

Of course, you can also get it based on the id attribute or other properties, such as $ ("input[id=id1]"). CSS ("color", red);
In jquery, when using $ ("Input[name= ' Metaid ']"). Val () cannot directly get the value of the selected radio, just get the first value of the radio tag, which may be relevant to jquery using the XPath language to find out. And we usually want to get the value of the selected radio, there are several ways:
1, use $ ("Input[name= ' Metaid ']:checked"). Val () Get//name Representative Name property name in radio
2, use $ (": radio:checked"). Val () Get//Limit page only a set of radio labels

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.