var exists = $('el').length > 0;
exists如果元素存在为 true | div.class检查是否存在的选择器 |
length将返回符合给定选择器的元素数量 |
示例
<!DOCTYPE html>
<html>
<head>
<title>检查指定元素是否存在-零五网(www.02405.com)</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<h2>检查指定元素是否存在-零五网(www.02405.com)</h2>
<div id="div_element"></div>
<button class="btn">检查</button>
<script>
$(document).ready(function(){
$(".btn").click(function(){
var exists = $('div.class').length > 0;
if(exists){
alert("元素存在");
} else {
alert("元素不存在");
}
});
});
</script>
</body>
</html>





