Events Twitter引导转盘:事件范围?
我有一个Twitter引导carousel.on(slided)事件,它是在单击针对特定幻灯片的元素后触发的,还有一个.on(slided)事件,它是在单击其他元素(close按钮)后触发的。但是,一旦第二个事件发生,即使第一个打开(滑动)事件也会继续触发。我原以为preventDefault()可以解决这个问题,但事实并非如此。我甚至尝试了一个更完整的函数(如下所示,从)来实现这一点,但没有成功 使用(此)将事件的范围与函数绑定在一起是行不通的。我对这一点相当陌生,所以我想这里可能有一个基本概念我没有掌握 以下是我的代码的简化版本:Events Twitter引导转盘:事件范围?,events,twitter-bootstrap,scope,carousel,Events,Twitter Bootstrap,Scope,Carousel,我有一个Twitter引导carousel.on(slided)事件,它是在单击针对特定幻灯片的元素后触发的,还有一个.on(slided)事件,它是在单击其他元素(close按钮)后触发的。但是,一旦第二个事件发生,即使第一个打开(滑动)事件也会继续触发。我原以为preventDefault()可以解决这个问题,但事实并非如此。我甚至尝试了一个更完整的函数(如下所示,从)来实现这一点,但没有成功 使用(此)将事件的范围与函数绑定在一起是行不通的。我对这一点相当陌生,所以我想这里可能有一个基本概
// Intended to prevent event bubble up or any usage after this is called.
eventCancel = function (e)
{
if (!e)
if (window.event) e = window.event;
else return;
if (e.cancelBubble != null) e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
if (window.event) e.returnValue = false;
if (e.cancel != null) e.cancel = true;
}
$('.recentContent').click(function(){
var $lastIndex = $('#splashCarousel .carousel-inner').children().last().getIndex();
$('#splashCarousel').carousel($lastIndex); //slide to specific index
$('#splashCarousel').carousel('pause');
$('#splashCarousel').on('slid', function (e) {
alert("open");
//do stuff
return eventCancel(e);
})
});
$('.closeX').click(function(){
var $lastItem = $('#splashCarousel .carousel-inner').children().last();
$('#splashCarousel').carousel(0);
$('#splashCarousel').on('slid', function (e) {
alert("close");
//do other stuff
return eventCancel(e);
})
});
这里有一个更简单的小提琴:
我可能做错了什么
谢谢
编辑:我在其他地方看到一些关于嵌套事件的讨论,它们不应该这样做,有时也不得不这样做。不管怎样,我似乎还是会被多次解雇。使用live()进行事件委派看起来很有希望。尽管如此,我还是需要一些反馈,因为这一切都开始让我感到困惑