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.
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($('#dom').exist()) //返回结果为 false console.log($('#mydom').exist()) //返回结果为 true console.log($('div').exist()) //返回结果为 true console.log($('p').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!