JQuery Learning (2) selector (1)

Source: Internet
Author: User

You can select DOM elements by Tag Name, attribute name, or content without browser compatibility issues.
Selector type:
Basic Selector
Level Selector
Filter Selector
Form Selector
Basic selector: the most frequently used selector in JQuery


Use JQuery:
Certificate ('background mydiv'background .css ('background-color', 'red ');
Use js:
Document. getElementById ('mydiv '). style. backgroundColor = 'red ';
According to JQuery's habits, the following code should be changed to the second method.
Element: matches all elements according to the given element name, that is, the element name after <
$ (Function (){
$ ('# But'). click (function (){
// Watermark ('background a1'background .css ('background-color', 'blue ');
Background ('a' background .css ('background-color', 'red ');
})
})
Matching Element by type: if multiple elements use the same type, select multiple elements.

 

Level Selector

 

Setting ('nvidivmid'demo.css ('display', 'block'): sets the layer visibility with id 'divmid'
$ ('Div span'mirror.css ('display', 'block'): All span elements in the layer with the id set to 'divmid' are visible, no matter how many levels of nesting
$ ('Div> span'demo.css ('display', 'block'): specify the level 1 span element in the layer with the id of 'divmid, the span element in this span element is invisible.

Dimensions ('nvidivmid1_div'mirror.css ('display', 'block'): sets the id to 'divmid' and the first div behind the layer to be visible.
Detail ('nvidivmid?span'demo.css ('display', 'block'): Set the id to 'divmid' and the first span is visible.
Nickname ('nvidivmid'0000.next().css ('display', 'block'): the first element after the layer with the id set to 'divmid' is visible, no matter what the element is

Dimensions ('nvidivmid').nextall().css ('display', 'block'): all elements behind the layer with the id set to 'divmid' are visible. No matter what elements are, they must be at the same level as divMid, the nested elements in a level element are invisible.
$ ('# DivMid ~ Div'demo.css ('display', 'block'): All the div elements behind the layer whose id is set to 'divmid' are visible and must be of a level.
$ ('# DivMid ~ Span'demo.css ('display', 'block');: All span elements after the layer whose id is set to 'divmid' are visible and must be of the same level.

Example:

[Javascript]
<Script src = "jquery/jquery-1.9.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
// Layered Selector
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
// $ ('# Divfirst span img'). hide (2000); // locate the img element under the span element whose id is divfirst
// $ ('# Divfirst img'). hide (2000); // locate all img elements whose id is divfirst
$ ('# Divfirst> span img'). hide (3000) // find the img element in the first span element with the id of divfirst! The first level refers to all the level-1 span elements under divfirst.
// $ ('# Divfirst + div img'). hide (3000) // find the img element next to its div element at the same level as the element whose id is divfirst
// $ ('# Divfirst ~ Div img '). hide (3000) // find the img element of all div elements whose id is divfirst
// $ ('# Divfirst'). next (). hide (3000); // find the first element at the same level as the element whose id is divfirst.
// $ ('# Divfirst'). nextAll (). hide (3000); // find all elements at the same level as the element whose id is divfirst
})
})
</Script>

<Script src = "jquery/jquery-1.9.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
// Layered Selector
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
// $ ('# Divfirst span img'). hide (2000); // locate the img element under the span element whose id is divfirst
// $ ('# Divfirst img'). hide (2000); // locate all img elements whose id is divfirst
$ ('# Divfirst> span img'). hide (3000) // find the img element in the first span element with the id of divfirst! The first level refers to all the level-1 span elements under divfirst.
// $ ('# Divfirst + div img'). hide (3000) // find the img element next to its div element at the same level as the element whose id is divfirst
// $ ('# Divfirst ~ Div img '). hide (3000) // find the img element of all div elements whose id is divfirst
// $ ('# Divfirst'). next (). hide (3000); // find the first element at the same level as the element whose id is divfirst.
// $ ('# Divfirst'). nextAll (). hide (3000); // find all elements at the same level as the element whose id is divfirst
})
})
</Script>


[Html]
<Body>
<Input id = "Button1" type = "button" value = "button"/>
<Div id = "divfirst">
<Span> Level 1 </span>
<Span> </span>
 
</Div>
<Div>

Layer 2
</Div>
<Div>

Layer 3
</Div>
<Div>

Layer 4
</Div>
</Body>

<Body>
<Input id = "Button1" type = "button" value = "button"/>
<Div id = "divfirst">
<Span> Level 1 </span>
<Span> </span>

</Div>
<Div>

Layer 2
</Div>
<Div>

Layer 3
</Div>
<Div>

Layer 4
</Div>
</Body>
Filter Selector

 

Matching elements based on certain filter rules

Example

[Javascript]
<Script src = "jquery/jquery-1.9.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// $ ('Img: eq (1) '). hide (3000); // Number
// $ ('Img: gt (0) '). hide (3000); // greater
// $ ('Img: lt (2) '). hide (3000); // less
// $ ('Img: even'). hide (3000); // even subscript
// $ ('Img: odd'). hide (3000); // odd subscript
// $ ('Img: not (# img1) '). hide (3000) // except that the ID is img1
// $ ('Img: not (. imgclass1) '). hide (3000) // except for imgclass1
 
})

<Script src = "jquery/jquery-1.9.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// $ ('Img: eq (1) '). hide (3000); // Number
// $ ('Img: gt (0) '). hide (3000); // greater
// $ ('Img: lt (2) '). hide (3000); // less
// $ ('Img: even'). hide (3000); // even subscript
// $ ('Img: odd'). hide (3000); // odd subscript
// $ ('Img: not (# img1) '). hide (3000) // except that the ID is img1
// $ ('Img: not (. imgclass1) '). hide (3000) // except for imgclass1

})
 

[Html]
<Body>




</Body>

<Body>




</Body>

 

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.