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

How does jquery determine whether a dom node exists?

王林
王林Original
2020-11-18 11:59:572565browse

How jquery determines whether a dom node exists: 1. Add the exists method to the jquery prototype; 2. Determine whether the length attribute of the current object is greater than 0 in the exists method. If it is greater, it exists; 3. Pass [ $('#id').exist()] just call it.

How does jquery determine whether a dom node exists?

The judgment idea is as follows:

(Learning video sharing: jquery video tutorial)

1 , first add an exist method on the jquery prototype;

2, then determine whether the length attribute of the current object is greater than 0 in the method, if it is greater than 0, it exists;

3, finally pass $('# Just call id').exist().

Add jquery extension js (directly write a separate js file to store the following code. And introduce it after the jquery code)

(function($) {
 $.fn.exist = function(){ 
  if($(this).length>=1){
   return true;
  }
  return false;
 };
})(jQuery);

Usage method:

The page has the following dom

<div id="mydom">这里是id=dom1节点</div>
<div>这里是DIV节点</div>
<span>这里是span节点</span>

Judgment:

console.log($(&#39;#dom&#39;).exist()) //返回结果为 false
console.log($(&#39;#mydom&#39;).exist()) //返回结果为 true
console.log($(&#39;div&#39;).exist()) //返回结果为 true
console.log($(&#39;p&#39;).exist()) //返回结果为 false

Related recommendations: js tutorial

The above is the detailed content of How does jquery determine whether a dom node 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