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

How does jquery determine whether a dom node exists?

王林
Release: 2020-11-18 11:59:57
Original
2608 people have browsed it

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);
Copy after login

Usage method:

The page has the following dom

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

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template