JQuery get object simple implementation method summary
- 2020-03-30 04:12:28
- OfStack
Monitor a container that pops up when the user clicks
The following code
$(function(){
$("Element").click{function(){
alert(" Click on me! ");
}
}
});
Basic object acquisition (note that these are Jquery objects and not Dom objects, but they can be converted)
The following code
$("*") 'means to get all objects But I haven't used it yet
$("#XXX") ' To obtain id=XXX Element object ( id It could be a label id or CSS style id ) The commonly used
$("input[name='username']") To obtain input In the label name='userName' Element object of The commonly used
$(".abc") ' Get the style class Is the name of .abc Element object of The commonly used
$("div") ' Label selector Select all div The element The commonly used
$("#a,.b,span") ' Said to get ID is (www.jb51.net)a The element and USES the class style b And all of them span The element
$("#a .b p") 'ID Number is a And used b The style of All of the p The element
case
Suppose you have the following code.
var target_obj = jQuery('#target_obj_id');
Then, if you need to determine whether the id is target_obj_id, you can implement the following two methods:
1.
The following code
if (target_obj.length > 0) { //If greater than 0 identifies the existence of an object with an id of target_obj_id, otherwise there is no
//Object existence processing logic
} else {
//Object does not exist in the processing logic
}
2,
The following code
if (target_obj[0]) {
//Object existence processing logic
} else {
//Object does not exist in the processing logic
}