Home  >  Article  >  Web Front-end  >  Summary of simple implementation methods for jQuery to obtain objects_jquery

Summary of simple implementation methods for jQuery to obtain objects_jquery

WBOY
WBOYOriginal
2016-05-16 16:32:291232browse

Monitor a container and pop up when the user clicks

The code is as follows

$(function(){
$("Element").click{function(){
alert("点击我哦!");
}
}
});

Basic object acquisition (note that the Jquery objects obtained here are not Dom objects, but they can be converted)

The code is as follows

Copy code The code is as follows:

$("*") ' means to get all objects, but I have never used it like this
$("#XXX") 'Get the element object with id=XXX (the id can be the tag's id or CSS style id) Commonly used
$("input[name='username']") Get the element object with name='userName' in the input tag. Commonly used
$(".abc") ' Get the element object whose name is .abc. Commonly used
$("div") ' Tag selector selects all div elements. Commonly used
$("#a,.b,span") ' means to get the element whose ID is (www.jb51.net)a, the element using class style b and all span elements
$("#a .b p") 'The ID number is a and uses b style for all p elements

Example

Suppose there is the following code.

Copy code The code is as follows:

var target_obj = jQuery('#target_obj_id');

Then, if you need to determine whether the id is target_obj_id, there are two ways to achieve it:

1,

The code is as follows

Copy code The code is as follows:

if (target_obj.length > 0) { //If greater than 0, the object with ID target_obj_id exists, otherwise it does not exist

//Processing logic for object existence

} else {

//Processing logic for object not existing

}

2.

The code is as follows

Copy code The code is as follows:

if (target_obj[0]) {

//Processing logic for object existence

} else {

//Processing logic for object not existing

}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn