Jquery .on(';鼠标指针';),函数(){…不工作

Jquery .on(';鼠标指针';),函数(){…不工作,jquery,swiper,Jquery,Swiper,我有以下事件: $('.swiper-slide-active').on('mouseenter', function(){ $(this).find('.overlay').addClass('show'); $(this).find('.hotspot').addClass('show'); }) 问题是当滑块准备就绪时会添加类.swiper slide active,但我认为用.on替换了.live事件。当您滚动包含该类的元素时,

我有以下事件:

$('.swiper-slide-active').on('mouseenter', function(){
            $(this).find('.overlay').addClass('show');
            $(this).find('.hotspot').addClass('show');
     })
问题是当滑块准备就绪时会添加类.swiper slide active,但我认为用.on替换了.live事件。当您滚动包含该类的元素时,不会发生任何事情。是否有方法等待添加该类

滑块的启动方式如下:

 var mySwiper = new Swiper('.swiper-container',{
            ....
          })
对动态元素使用委派的
on
事件:

$(document).on('mouseenter', '.swiper-slide-active', function(){
        $(this).find('.overlay').addClass('show');
        $(this).find('.hotspot').addClass('show');
 })
它的工作原理是监听事件(在本例中为
mouseenter
)冒泡到未更改的父级(
document
是默认值,如果您没有更接近更改的元素来挂起它),然后应用jQuery筛选器,然后针对导致事件的任何匹配元素运行提供的函数

如果您的箱子可能会挂在
.swiper容器上
,以提高效率:

e、 g

*注意:避免使用
“body”
来挂起委派事件,而不要使用
文档
,因为
body
与委派事件有一些与样式相关的问题。

使用
$(文档)。就绪(处理程序)


应该是这样的

$(document).on('mouseenter', '.swiper-slide-active', function(){
// Bind
$( ".swiper-slide-active" ).on( "click", function( e ) {} );
$( ".swiper-slide-active" ).bind( "click", function( e ) {} );

// Live
$( document ).on( "click", ".swiper-slide-active", function( e ) {} );
$( ".swiper-slide-active" ).live( "click", function( e ) {} );
bind
live
on
的区别如下

$(document).on('mouseenter', '.swiper-slide-active', function(){
// Bind
$( ".swiper-slide-active" ).on( "click", function( e ) {} );
$( ".swiper-slide-active" ).bind( "click", function( e ) {} );

// Live
$( document ).on( "click", ".swiper-slide-active", function( e ) {} );
$( ".swiper-slide-active" ).live( "click", function( e ) {} );

问题是滑块动态添加类,而不是就绪事件。@此代码与此$(文档)的工作原理相同。在('mouseenter',Nope as
。滑块组件(而非文档)完成之前,swiper slide active
不存在。请重新阅读问题:
“滑动条准备就绪时,会添加类.swiper slide active”
ok,如果我们想使用,实际上我们需要这个函数。在已经存在的类或id上的函数上。这里的情况不同。在滑动条准备就绪后,类会动态添加。正确。您需要一个附加到非更改祖先的委派事件处理程序。