
jquery如何判断节点是否存在?
想要判断某个dom节点是否存在,可以使用length属性来判断。
例:
if($('.onloadMore').length>0){
return '节点存在';
}else{
return '节点不存在';
}可以看出:length 属性包含 jQuery 对象中元素的数目;如果某个dom节点对象的length属性大于等于1,则存在。
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="a">这里是id=a节点</div>
<div>这里是DIV节点</div>
<div>这里是DIV节点</div>
<span>这里是span节点</span>
<script>
(function($) {
$.fn.exist = function() {
if($(this).length >= 1) {
return true;
}
return false;
};
})(jQuery);
alert("#aaa是否存在:"+$('#aaa').exist()); // false
alert("#a是否存在:"+$('#a').exist()); // true
alert("div是否存在:"+$('div').exist()); // true
alert("p是否存在:"+$('p').exist()); // false
</script>
</body>
</html>效果图:




更多web前端知识,请查阅 HTML中文网 !!
以上就是jquery如何判断节点是否存在?的详细内容,更多请关注易知道|edz.cc其它相关文章!












