jquery Selector, attribute setting usage experience Summary _jquery

Source: Internet
Author: User
I am a small white, fresh graduates, previously used jquery, the recent project to use jquery. In the process of doing a lot of detours, non-stop search. Summed up a number of uses for your reference:

The most basic selector syntax includes: ID, class, label, attribute, which is consistent with the CSS selector.

ID selector to add # before ID, such as to select a DIV element with ID mydivid (<div id= "Mydivid" ></div>) can write this:
Copy Code code as follows:

$ ("#myDivID");

D is not repeatable, so the ID selector selects a jquery object.

The class selector points (.) in front of class, for example, to select an INPUT element with class Myinputclass (<input type= "text" class= "Myinputclass"/>) can be written like this:
Copy Code code as follows:

$ (". Myinputclass");

Class can be repeated, so the class selector can be a class of elements, that is, a good number of elements, so the jquery selection is the array, you can refer to the subscript to select each element:
Copy Code code as follows:

for (var i = 0; I < $ (". Myinputclass"). Length; i++) {$ (". Myinputclass") [I];}

This allows you to iterate through each element.

Tag Selector directly write tag type, such as to select a paragraph p tag (<p></p>) can write:
Copy Code code as follows:

$ ("P");

The tag Selector is also an array, selecting all the P tag elements, or iterating through all the elements in the above method.

The property selector is preceded by a square bracket ([]), such as to select an element containing Name= "Xxname", which can be written like this:
Copy Code code as follows:

$ ("[Xxname]");

In this case, the selection is also an array, because name can be duplicated.

ID selector can select an element accurately, but in development we may have to choose a set of elements, how to accurately select the elements we want, in fact, several selectors can be mixed use:
Copy Code code as follows:

<div id= "Attrvaluetab" >
<span style= "White-space:pre" > </span><p>
<span style= "White-space:pre" > </span><input type= "button" value= "OK"/> <input "text" value = "Odd"/> <input type= "text" value= "even"/>
<span style= "White-space:pre" > </span></p>
<span style= "White-space:pre" > </span><p>
<span style= "White-space:pre" > </span><input type= "button" value= "Cancel"/> <input "text" value = "Odd"/> <input type= "text" value= "even"/>
<span style= "White-space:pre" > </span></p>
</div>


For example, we want to select an even number of text labels, that is, a even text box. We can choose this way:

First select the div, and then select P, and then select the text box type= "text", and then select an even number of places:
Copy Code code as follows:

$ ("#attrValueTab p input[type= ' text ']:even");

Portfolio selection is very useful in development. You can use the following method to select a button that is checked or a checkbox element:
Copy Code code as follows:

$ ("Input[name= ' Avdefvalue_input ']:checked");

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.