![](/img/trans.png)
[英]How to hide a parent div if an inner div has a certain class, with javascript
[英]How to check if any div has inner div having specific class say 'xyz' using javascript?
<div class="abc">
<a><img></a>
<h4></h4>
<div class="xyz">
<a href="google.com">Hello</a>
</div>
</div>
在上面的html代碼如何檢查是否div
上課abc
已經div
上課xyz
。
您可以簡單地做到這一點:
var list = document.querySelectorAll('div.abc div.xyz');
if (1 == list.length) {
alert("found");
}
這是一個演示: http : //jsfiddle.net/3xQ5X/
使用JQuery:
$("div.abc").has("div.xyz");
嘗試如下操作:為您的父div分配一個abc等ID。
var v = document.getElementById('abc');
for(var i in v.children)
{
if( v.children[i].nodeName == 'DIV')//this will tell if the parent div has children divs
{
console.log(v.children[i].className == 'xyz');//this will be true if the child div has a class named xyz.
}
}
還記得根據您的要求修改此腳本。 我的意思是,您可以給要遍歷的div指定一個特定的類來代替id。 要選擇包含某個特定類的所有div,請使用此鏈接的函數。
這個腳本會做有需要的。
<script type="text/javascript">
$(document).ready(function(){
if($("div.abc").children('div').hasClass("xyz"))
{
alert("found");
}
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.