jQuery .click() 轮播内的项目
我在轮播中有一个项目,并将 .click() 事件绑定到它。它工作得很好,因为该项目位于轮播的第一页中,并且在加载时可见。
但是,如果我移动轮播使其变得不可见,然后移回到视口中,则 click() 事件不再起作用?
当它可见时,我可能只需要以某种方式再次注册它?
BTW Click 用于打开模态 jQuery UI 对话框
谢谢
I have an item inside a carousel and tied .click() event binding to it. And it is working all great, because that item is in the first page of carousel and visible right on load.
But if I move the carousel so that it becomes invisible and then move back in the viewport, the click() event isn't working anymore?
I probably just have to register it somehow again when it is visible?
BTW Click is used to open a modal jQuery UI dialog
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的评论,我确实注意到,当您循环浏览轮播时,点击不再有效。
我不知道这个插件具体是如何工作的。我想它可能会重新创建元素,并且事件处理程序会丢失。
解决此问题的一种方法是使用事件委托。
基本上,您会将
.quickQoute
上的点击的执行委托给容器,在您的情况下是轮播元素#inner
:有关使用 .on()
Following you comment, I noticed indeed that the click was not working anymore when you looped through the carousel.
I don't know exactly how this plugin works. I suppose it may re-creates the elements and the event handler is lost in the way.
An approach to resolve this is to use event delegation.
Basically you will delegate the execution of the click on
.quickQoute
to the a container, in your case the carousel element#inner
:More on event delegation with .on()
我建议使用 liveQuery 插件
这会在创建元素时自动将事件绑定到元素。例如:
这允许您首先设置事件处理程序,并可以选择稍后创建元素,就像您的旋转木马一样。
I suggest using the liveQuery plugin
This will automatically bind events to elements when they are created. For example:
This allows you to set event handlers first, and have the option to have elements created at a later time, like your carrousel does.