[英]Hide a div when another div is empty
I know this question has been asked in other topics but for some reason I can't get this code to work on my WordPress website. 我知道这个问题已经在其他主题中提出过,但是由于某种原因,我无法在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>
I really don't understand why #objectB is not hidden on my page. 我真的不明白为什么#objectB没有隐藏在我的页面上。 When I check the code with developer tools I can see it's there.
当我使用开发人员工具检查代码时,我可以看到它在那里。
Try replacing: 尝试更换:
if ($(".objectA").html().length == 0) {
$("#objectB").hide();
}
With: 附:
if (!$(".objectA").html()) {
$("#objectB").hide();
}
try adding document ready function. 尝试添加文档准备功能。 no need to click, document ready runs after page loads.
无需单击,页面加载后即可运行文档准备工作。
$( document ).ready(function() {
if ($(".objectA").html().length == 0) {
$("#objectB").hide();
}
});
After Googling a bit more I found the solution myself. 进一步搜索之后,我自己找到了解决方案。
WordPress doesn't accept just if($
... It needs jQuery(function ($) {
before the function. Like this: 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.