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