Read jquery three (build selector) _jquery

Source: Internet
Author: User
In order to describe the focus of each article, the sample code is the most concise, such as selectors can only pass HtmlElement and IDs.

This article we enhance the next selector, according to the 2/8 principle, here only to implement the most commonly used.


1, obtained by ID, the element is unique
$ (' #id ')

2, get through classname
$ ('. CLS ') gets all classname elements in the document that are CLS-compliant
$ ('. CLs ', EL)
$ ('. CLS ', ' #id ')
$ (' span.cls ') gets all classname-CLS-compliant span elements in the document
$ (' Span.cls ', el) gets the element classname as CLS in the specified element, El is htmlelement (not recommended)
$ (' span.cls ', ' #id ') gets the element classname as CLS in the element of the specified ID

3, get through tagname
$ (' span ') get all span elements in the document
$ (' span ', EL) gets the span element in the specified element, El is htmlelement (not recommended)
$ (' span ', ' #id ') gets the span element in the element with the specified ID

4, acquired by attribute
$ (' [name] ') gets the element with attribute name in the document
$ (' [name] ', EL)
$ (' [name] ', ' #id ')
$ (' [name=uname] ') Gets the elements of all the properties Name=uname in the document
$ (' [Name=uname] ', EL)
$ (' [name=uname] ', ' #id ')
$ (' input[name=uname] ') gets the input element for all properties in the document Name=uname
$ (' input[name=uname] ', EL)
$ (' input[name=uname] ', ' #id ')

Example:

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>zchain test</title>
<script src= "Http://demo.jb51.net/js/2011/zchain/zchain-0.3.js" ></script>
<body>
<div id= ' content ' >aaa</div>
<div>bbb</div>
<p class= "PRA" >ccc</p>
<input type= "Submit" value= "Submit"/>
<input type= "button" value= "Submit"/>
<script type= "Text/javascript" >
var obj1 = $ ("#content"); Id
var obj2 = $ (' div '); TagName
var obj3 = $ ('. Pra '); ClassName
var obj4 = $ (' Input[type=button] '); Attribute

Console.log (OBJ1);
Console.log (OBJ2);
Console.log (OBJ3);
Console.log (OBJ4);
</script>
</body>

Firebug output is as follows


Http://demo.jb51.net/js/2011/zchain/zchain-0.3.js

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.