Some knowledge points in JQuery

Source: Internet
Author: User

1. Use JQuery to check whether an object exists on the webpage:

How to determine whether an object exists? When you call methods such as text (), html (), and click (), each method is called iteratively for each DOM object in the array, therefore, even if the element selected by id does not exist, no error is returned. If you need to determine whether the specified id exists, you should write:

 

// Determine whether an object exists by judging the number of objects in the $ ("# btn1") Collection (feasible)

If ($ ("# btn1"). length <= 0 ){

Do something

}

 

// Directly determine whether a DOM object exists (feasible)

If ($ ("# btn1") [0]) {

Do something

}

 

//

If ($ ("# btn1 ")){

Do something

}

 

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

 

<Head>

 

<Title> </title>

 

<Script src = "../Scripts/jquery-1.4.1.js" type = "text/javascript"> </script>

 

<Script type = "text/javascript">

 

$ (Function (){

 

$ ('# Allornone'). click (function (){

 

// Click "select status" to select all or none

 

$ (': Checkbox'). attr ('checked', $ (this). attr ('checked '));

 

});

 

 

 

// When the selected status of the selected item is changed, the "select status" status also changes.

 

$ (': Checkbox [id! = AllOrNone] '). click (function (){

 

OptionStateChanged ();

 

 

});

 

 

 

 

 

// Option to change the abstract Method

 

Function optionStateChanged (){

 

Var state = true;

 

$ (': Checkbox [id! = AllOrNone] '). each (function (){

 

If (! $ (This). attr ('checked ')){

 

State = false;

 

Return false;

 

// Break; break cannot be used;

 

}

 

});

 

// If one of them is not selected, set the "select status" status to "not selected ".

 

$ ('# Allornone'). attr ('checked', state );

 

}

 

 

 

// Invert Selection

 

$ ('# ChooseReverse'). click (function (){

 

// Select multiple options.

 

$ (': Checkbox [id! = AllOrNone] '). each (function (){

 

$ (This). attr ('checked ',! $ (This). attr ('checked '));

 

});

 

// Separately consider the option change operation

 

OptionStateChanged ();

 

});

 

});

 

</Script>

 

</Head>

 

<Body>

 

<Input type = "checkbox" value = "1"/> football

 

<Input type = "checkbox" value = "2"/> football

 

<Input type = "checkbox" value = "3"/> football

 

<Input type = "checkbox" value = "4"/> football

 

<Input type = "checkbox" value = "5"/> football <br/>

 

<Input type = "checkbox" name = "name" value = "" id = "allOrNone"/> select status

 

<Input type = "button" name = "name" value = "" id = "chooseReverse"/>

 

</Body>

 

</Html>

Related Article

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.