JS隐藏div,如果它有通过JS添加的类 [英] JS hide div if it has a class added via JS

查看:88
本文介绍了JS隐藏div,如果它有通过JS添加的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个代码,其中活动类通过JS添加到div。



我想要做的是,当div有 active 类时,隐藏另一个div。但是,由于活动类是通过JS添加的,所以此代码无效:

  if($(#section3)。hasClass(active)){
$(。menu)。fadeOut(fast);



$ b我想我需要使用on函数:。on()



类似这样的,但它不是工作...任何想法?

  $(#section3)。on(function(){
$ (this).hasClass('active'){
$(。menu)。fadeOut(fast);
}
});

编辑:



恐怕我无法粘贴代码,因为我正在使用插件。 这是我正在使用的,所以你可以看到功能在那里。



我已经添加了这个项目符号菜单:

 < ; ul id =menu> 
< li data-menuanchor =slide1>< a href =#slide1>< span>< / span>< / a>< / li>
< li data-menuanchor =slide2>< a href =#slide2>< span>< / span>< / a>< / li>
< li data-menuanchor =slide3>< a href =#slide3>< span>< / span>< / a>< / li>
< li data-menuanchor =slide4>< a href =#slide4>< span>< / span>< / a>< / li>
< / ul>

每张幻灯片都有一个活动类在视口中,所以我想实现的是当最后一个滑块处于活动状态时,隐藏菜单

解决方案

li有活动类,所以你可以使用类似的东西,或者如果它依赖于幻灯片显示,那么可以使用 afterSlideLoad 事件



< $($#$>)$($#$)$($)$($) )){
(#menu)。fadeOut(fast)
}
})

或者您可以使用slideIndex检查您是否位于最后一张幻灯片上。看到这里> afterSlideLoad


I am working on a code where an active class is added to a div via JS.

What I would like to do is, when that div has active class, hide another div. But, due to the active class was added via JS, this code doesn't work:

if($("#section3").hasClass("active")) {
    $(".menu").fadeOut("fast");
}

I think I would need to use the on function: .on()

Something like this, but it's not working... Any ideas?

$("#section3").on( function() {
    $(this).hasClass('active') {
        $(".menu").fadeOut("fast");
    }
});

EDITED:

I'm afraid I cannot paste the code because I am using a plugin. This is the one that I am using, so you can see the functionality there.

I've added this bullet menu to it:

<ul id="menu">
    <li data-menuanchor="slide1"><a href="#slide1"><span></span></a></li>
    <li data-menuanchor="slide2"><a href="#slide2"><span></span></a></li>
    <li data-menuanchor="slide3"><a href="#slide3"><span></span></a></li>
    <li data-menuanchor="slide4"><a href="#slide4"><span></span></a></li>
</ul>

Each slide has a active class when it's on viewport, so what I would like to achieve is when the last slider is active, hide the menu

解决方案

oth the section and the li have active classes so you could use something like this or use the afterSlideLoad event if it is depeding of the slideshow

$(window).on("scroll",function() {
  if ($('#menu li:last-child').hasClass("active")) {
    ("#menu").fadeOut("fast")
  }
})

or you can use slideIndex to check if you are on the last slide. see here > afterSlideLoad

这篇关于JS隐藏div,如果它有通过JS添加的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆