$ Selector usage in JQuery

Source: Internet
Author: User

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 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 that are not equal to the specified 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 = "edevil Plans"/>
JQuery code:
$ ("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:
<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 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:
<Input name = "newsletter"/>
<Input name = "milkman"/>
<Input name = "newsboy"/>
JQuery code:
$ ("Input [name ^ = 'News']")
Result:
[<Input name = "newsletter"/>, <input name = "newsboy"/>]

In jquery, when $ ("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

Related Article

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.