我有一个Twitter引导轮播.on(滑动)事件,在单击定位特定幻灯片的元素后触发,以及单击其他元素后触发的.on(滑动)事件(关闭按钮)。但是,一旦第二个事件发生,即使第一次打开(滑动)事件,它也会继续发射。我认为preventDefault()会解决这个问题,但事实并非如此。我甚至尝试了一个更完整的功能(如下所示,来自Herb Caudill)来做到这一点,但是没有去。
使用(this)将事件的范围绑定到函数不起作用。我对此很新,所以我认为这里可能存在一个基本概念,我不会理解。
以下是我的代码的简化版本:
// 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);
})
});
这是一个更简单的小提琴尝试preventDefault:http://jsfiddle.net/chardwick/a5Dpe/
我可能做错了什么?
谢谢!
编辑:我在其他地方看到有关嵌套事件的讨论,两者都不应该完成,而且有时他们必须这样做。无论哪种方式,我似乎仍然得到多个事件发射。使用live()进行事件委派似乎很有希望。尽管如此,仍然可以使用一些反馈,因为它开始让我感到困惑。