我正在努力让以下工作。我可以使用:has
而不是三个来执行2个ID。我希望只有在span_element
的孙子时才能将CSS应用于disabling_div
。
的jQuery
$("[id^=span_element]:has[id^=another_id]:has([id^=DisablingDiv])").css({ "cursor": "not-allowed" });
或
$("[id^=span_element].find([id^=DisablingDiv])").css({ "cursor": "not-allowed" });
HTML
<div id="span_element_1"> <!--apply style to this, only when DisablingDiv_1 is below-->
<div id="another_id">
<div id="DisablingDiv_1">
Content
</div>
</div>
</div>
答案 0 :(得分:1)
使用查找和长度
if( $('[id^=span_element]').find('[id^=DisablingDiv]').length!==0 ){
alert("Yes! #span_element_1 contains a #DisablingDiv_1");
// And here your magix, like showing the span.
}
这会尝试找到#span_element_1
,然后它会尝试查找#DisablingDiv_1
作为其中一个后代。如果它找到多于零的匹配,则它存在。
答案 1 :(得分:1)
>
作为直接子选择器
$("[id^=span_element] > > [id^=DisablingDiv]").css({
"color": "red"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="span_element_1">not red
<div id="another_id"> not red
<div id="DisablingDiv_1">
red
</div>
</div>
</div>
答案 2 :(得分:1)
在阅读完您的编辑和一些评论后,我认为您会搜索此内容:
$("[id^=DisablingDiv]").parent().css(); // That's #another_id
$("[id^=DisablingDiv]").parent().parent().css(); // That's #span_element_1
这样你就走到了底部&gt;顶部,您不必验证#span_element或another_div是否包含DisablingDiv。
答案 3 :(得分:1)
$("[id^=span_element]").find("[id^=DisablingDiv]").css({ "cursor": "not-allowed" });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="span_element_1"> <!--apply style to this, only when DisablingDiv_1 is below-->
<div id="another_id">
<div id="DisablingDiv_1">
Content
</div>
</div>
</div>
答案 4 :(得分:0)
<div class="wrapper">
<div id="span_element_1"> <!--apply style to this, only when DisablingDiv_1 is below-->
<div id="another_id">
<div id="DisablingDiv_1" class="active">
Content
</div>
</div>
</div>
</div>
if ( $("[id^=span_element].parents('.wrapper').find('#another_id').hasClass("active)") {
$("[id^=span_element].css(); // Do css if active class present
} else {
$("[id^=span_element].css(); // Do css if active class not present
}