The selector part of
$:
Whenever $ is used, the return value is an object. The
$ selector is mainly used to select tags. The basic usage is the same as the CSS selector. But , what is very exciting is that it supports common browsers, and many selectors in CSS are not supported by IE6.
1. Basic selectors (3 types): ----Any use of $, the other The return value is an object
$("label name"), such as $("p") selects all p label nodes
$("#id name"), such as $("#test" ) selects the label node
$(".class name") with the id of test. For example, $(".test") selects all the label nodes
with the class of test. The $("label name" above ") and $(".class name") return all satisfied nodes. For further filtering, you can add some functions, such as eq, gt, lt, etc.
2. Group selector: ---- A combination of 3 basic selectors, separated by ","
Let's make a convention: write "tag name or #id name or .class name" as mix, then mix represents a tag name, or a # id or a .class.
$("mix,mix,mix,..."), such as:$("div,#test1,p,.test2,#test3")
3 .Descendant selector: ----A combination of 3 basic selectors, separated by " ", all direct ones are valid
$("mix mix"), of course it can be multiple nested, but the descendant selector can be Deep descendants, so $("mix mix mix...") is not very useful. Example: $("div .test"): all descendant elements of the class with test in the div tag (that is, the The nested class attribute of the div is the test tag)
4. Child selector: ----A combination of 3 basic selectors, separated by ">", only valid for parents and children
$( "mix>mix"), this is placed after the descendant selector for comparison. The child selector can only select the first generation of descendants. It does not handle deep nesting. Example:
$("div>. test")
is valid for the p paragraph tag here. But for
is invalid for the p paragraph tag here, use
$("div .test)
5. Adjacent selector: ----A combination of 3 basic selectors, separated by " ", only the next sibling node
$("mix mix"), selects the next sibling node. For example: $(" div #test"), the node with id test must be the next sibling node of div.
in $ ("div #test") can get the p paragraph node
then Cannot be obtained
6. Attribute selector: ---- " |=|^|$|*"
The reason why the attribute selector is not placed in the css selector is because the writing method in jQuery is different. As for the writing method in css, you can refer to a CSS selector article I wrote before. The writing method in jQuery is similar to xPath:
$("mix[@attr]"): Select all the mix and have the attr attribute Node
$("mix[@attr=a_value"]): Select all nodes
$("mix[@attr^=a_value_head"]) of this mix that have the attr attribute and satisfy the attribute value as a_value: The attribute value of the attr attribute starts with a_value_head
$("mix[@attr$=a_value_end"]): The attribute value of the attr attribute ends with a_value_end
$("mix[@attr*=a_value "]): The attribute value of the attr attribute contains a_value
7. Limited selector:
This name is my own. In fact, selector combinations have limited meanings. You will understand what will be introduced later. The knowledge is enough.
Has a qualified child node selector: $("mix1[mix2]"): Returns the mix1 node containing mix2. For example: $("div[a]"): A div containing the a tag.
This is different from $("div a"). The latter represents the a tag in the div and returns the a tag object. The former returns the div tag object.
Colon-qualified node selector: $( "mix:condition"): mix tag, and meet the qualifying conditions.
E:root: The type is E, and it is the root element of the document
E:nth-child(n): It is the th-th element of its parent element n child elements of type E, the base number starts from 1
E: first-child: It is the first child element of type E of its parent element
E:last-child: It is the first child element of its parent element The last child element of type E
E:only-child: and is the only child element of type E of its parent element