【问题标题】:Hover not working properly in jquery悬停在jquery中无法正常工作
【发布时间】:2017-06-01 12:46:41
【问题描述】:

我的 jquery 悬停在桌面视图中有问题。每次我悬停(鼠标移出)父导航时,子页面都不会关闭,在我悬停后,子页面保持打开状态。如何解决这个悬停问题,当我每次悬停时子页面也会隐藏。

我正在使用此代码

jQuery('.dropdown').hover(function(){
    if(!jQuery('.navbar-toggle').is(':visible')) { // disable for mobile view
        if(!jQuery(this).hasClass('open')) { // Keeps it open when hover it again
            jQuery('.dropdown-toggle', this).trigger('click');
        }
    }
});

请查看图片以获得更好的可视化效果

【问题讨论】:

  • 能否包含相关的 HTML 和 CSS?

标签: javascript jquery css hover


【解决方案1】:

mouseout 不能自动返回。你需要像这样分配mouseout 的一些功能

jQuery('.dropdown').hover(function(){
    if(!jQuery('.navbar-toggle').is(':visible')) { // disable for mobile view
        if(!jQuery(this).hasClass('open')) { // Keeps it open when hover it again
            jQuery('.dropdown-toggle', this).trigger('click');
        }
    }
});
jQuery('.dropdown').mouseout(function(){ // for mouse out event
//do stuff
})

hover()

jQuery('.dropdown').hover(function(){
        if(!jQuery('.navbar-toggle').is(':visible')) { // disable for mobile view
            if(!jQuery(this).hasClass('open')) { // Keeps it open when hover it again
                jQuery('.dropdown-toggle', this).trigger('click');
            }
        }
    },function(){ // for mouse out event
        jQuery(this).removeClass('open')
    })

工作示例

$('a').hover(function(){
$(this).css('color','red')
},function(){
$(this).css('color','black')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a>ppp</a>

【讨论】:

  • 是的,我打算提出同样的建议
  • 为什么不给悬停的out-action分配一个处理程序?调用 $( selector ).hover( handlerIn, handlerOut ) 是以下的简写: $( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );
  • @KeAn .nice 。我在jquery 中不太好。因为我的方法是mouseout .gentral 来自本机js 的概念
  • 我应该在 jQuery('.dropdown').mouseout(function(){ 上放什么?抱歉这个菜鸟问题
  • @MelvinR​​ey 你可以用or 悬停方法来做。我只应用removeClass('open')。我在没有看到你的sn-p html代码的情况下隐藏受尊敬的元素是不正确的。只是尝试和如果不发布sn-p html代码
猜你喜欢
  • 1970-01-01
  • 2016-04-27
  • 2023-04-01
  • 1970-01-01
  • 2011-09-13
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多