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>