jquery selector Easy to overlook the big problem of small knowledge

Source: Internet
Author: User

1

About checking the existence of an element on a Web page

Many people will write 1 if($ ("#tt")) { 2 //dosomething 3 }

In fact, it should be judged based on the length of the element obtained.

if ($ ("#tt"). Length>0) {//do something blog Park How to modify my tags }

Of course, it can also be converted into DOM objects to determine

if ($ ("tt") [0]) {//dosomethign}

2

About child element Filter selectors: Nth-child (index/even/odd/equation) and: EQ (index) should be used to note that the value of the former is calculated from 1, the latter starting from 0

The former selects the index child element or the odd and even element (Odd/even) under each parent element and: EQ (index) can only match one element.

Eg: nth-child (3n+1 ') can select an element with the index value under each parent element (3N+1) (remember that n is starting from 1 Oh ~)

3

When using: Input selector It is important to note that: input is a low-key God, he will not only choose <input>, and will also steal the magic of the choice <textarea><select><button>

4

The $ ("#form: Input") we used to count the number of elements in the form. length;

Very easy feet have wood, but a lot of people will subconsciously write $ ("#form1 input"). length~

I can't help but say it. descendant selectors and filter selectors it's easy to get the wrong twins, too.

var $hd =$ ('. Text:hidde '); // Note that there is a space after the text, and others choose the element that is class "text" in the hidden element   filter selector (there must be a space to filter open bar ~)var $gl =$ ('. Text:hidden ') // Well, this is the selection of the Hidden class "text" element, the authentic descendant selector

5

Finally write your own discovery ~ that is, compared with if else, perhaps we can use the toggle () method to alternate a set of actions

eg

 Togglebtn.click (function   () { Span style= "color: #0000ff;" >if  ($category. Is (": Visible"  //   hide some elements    else  //  display some elements   
$toggleBtn. Toggle (function() {    // light a bar}            funcrtion () {     // or is it good to hold the lute half covered? }) // two function is OK

jquery selector Easy to overlook the big problem of small knowledge

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.