Wordpress 中的 jQuery - 如果父 div 包含特定的其他 div,则隐藏父 div
如果没有幻灯片,我试图隐藏光滑的滑块部分。我尝试了很多不同的选项,比如尝试使用 PHP 和 CSS,但我觉得我最接近让它与 jQuery 一起使用。
HTML 输出结构是:
<div class="container-flex type-testimonials-container">
<div class="container">
<div class="container type-testimonials slick-initialized slick-slider">
<div class="slick-list draggable">
<div class="slick-track">
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
</div>
</div>
</div>
</div>
</div>
所以,我想如果单个幻灯片 div (.slick-slide) 不存在,我可以使用 jQuery 隐藏包含的 div (.type-testimonials-container)。
我已经尝试过以下操作:
if(jQuery(".slick-slide").html().length)
{
jQuery(".type-testimonials-container").hide();
}
以及许多变体...我认为这可能是因为两个 div 不在同一级别,并且一个包含另一个,但试图找到一种父/子方式事实证明很困难...我不知道该走哪条路...
任何帮助将不胜感激!
编辑*
我也尝试过检查父子关系并尝试等待 DOM 加载,就像这样:
document.addEventListener("DOMContentLoaded", function(event) {
var parentDiv = document.getElementsByClassName("slick-track");
var childDiv = document.getElementsByClassName("slick-slide");
if (parentDiv.contains(childDiv))
{
alert("div DOES exist");
}
else{
alert("div DOES NOT exist");
}
});
但这只是向我显示了不存在警报,即使它确实存在 - 这会在整个 DOM 中搜索它吗?或者我是否需要提供 div 从 body 或其他东西的确切路径?
I'm trying to hide a slick slider section if it has no slides. I've tried tons of different options, like trying to use PHP and CSS, but I feel I'm closest to getting it to work with jQuery.
The HTML output structure is:
<div class="container-flex type-testimonials-container">
<div class="container">
<div class="container type-testimonials slick-initialized slick-slider">
<div class="slick-list draggable">
<div class="slick-track">
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
<div class="slick-slide">
SINGLE SLIDE CONTENT
</div>
</div>
</div>
</div>
</div>
</div>
So, I'm thinking I can use jQuery to hide the containing div (.type-testimonials-container) if the single slide div (.slick-slide) doesn't exists.
I have tried the following:
if(jQuery(".slick-slide").html().length)
{
jQuery(".type-testimonials-container").hide();
}
As well as lots of variations of that... I think it might be because the two divs aren't on the same level and one contains the other, but trying to find a parent/child way of doing is proving difficult... I'm not sure which way to go...
Any help would be massively appreciated!
EDIT*
I've also tried checking the parent and child relationship and trying to wait until the DOM has loaded, like this:
document.addEventListener("DOMContentLoaded", function(event) {
var parentDiv = document.getElementsByClassName("slick-track");
var childDiv = document.getElementsByClassName("slick-slide");
if (parentDiv.contains(childDiv))
{
alert("div DOES exist");
}
else{
alert("div DOES NOT exist");
}
});
But this just shows me the DOES NOT exist alert even though it does exist - Will this search the whole of the DOM for it? or do I need to provide the exact path of the div from body or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接查询
.slick-slide
的 HTML 集合的长度?如果目标元素不存在,JQ 仍然会返回一个对象,并且该对象将具有属性length
。像这样的东西Why not just query for the length of the HTML collection of
.slick-slide
? JQ will still return an object if the target element doesn't exist, and the object will have a propertylength
. Something like我设法这样做:
I managed to do it this way: