Home  >  Article  >  Web Front-end  >  How does jquery determine whether a dom element exists?

How does jquery determine whether a dom element exists?

青灯夜游
青灯夜游Original
2021-11-12 18:44:003700browse

Jquery method to determine whether a dom element exists: 1. Use the "$("#id")" statement to obtain the dom element object through the id attribute; 2. Use the "element object.length" statement to obtain the length value; 3. Determine whether the obtained length value is greater than 0. If it is greater, it exists, otherwise it does not exist.

How does jquery determine whether a dom element exists?

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery determines whether the dom element exists

Implementation idea:

  • Pass in the js file The id attribute gets the dom element object

  • Use the length attribute to get the length of the dom element object, If the element exists, length=1

  • Determine whether the length value is greater than 0

##Implementation code:

if($("#id").length >0){
//元素存在,则length=1
 alert("元素存在");
}else{
 
  //元素不存在,则length=0
 alert("元素不存在");
}

Recommended related video tutorials:

jQuery tutorial (video)

The above is the detailed content of How does jquery determine whether a dom element exists?. For more information, please follow other related articles on the PHP Chinese website!

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