[英]Hide a div when another div is empty
我知道这个问题已经在其他主题中提出过,但是由于某种原因,我无法在WordPress网站上使用此代码。
if ($(".objectA").html().length == 0) { $("#objectB").hide(); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="objectA"></div> <div id="objectB">hide this div</div>
我真的不明白为什么#objectB没有隐藏在我的页面上。 当我使用开发人员工具检查代码时,我可以看到它在那里。
尝试更换:
if ($(".objectA").html().length == 0) {
$("#objectB").hide();
}
附:
if (!$(".objectA").html()) {
$("#objectB").hide();
}
尝试添加文档准备功能。 无需单击,页面加载后即可运行文档准备工作。
$( document ).ready(function() {
if ($(".objectA").html().length == 0) {
$("#objectB").hide();
}
});
进一步搜索之后,我自己找到了解决方案。
WordPress不仅仅接受if($
...,它需要在函数前使用jQuery(function ($) {
。
jQuery(function ($) {
if($(".empty-div").html().length ==0)
{
$("#deals-wrapper, .listing-details li.vc_tta-tab:nth-child(n+3):nth-child(-n+3)").hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.