我創建了一個名爲slides.swap
的自定義事件,但是我想阻止trigger()
中click
發生的事件。它不工作。我如何從trigger()
那裏做到這一點? 我知道我可以阻止它在click
函數中,但我只想保留所有「封裝」。是否可以在觸發器中使用.preventDefault()jQuery
JS:
tabs.on('click', 'a', function(e){
$(this).trigger('slides.swap', e);
});
//Custome event
tabs.find('a').bind('slides.swap', function(e){
var self = $(this),
selfIndex = self.parent().index(),
targetSlide = slides.eq(selfIndex);
e.preventDefault(); //somehow this doesn't work
//fade in/out slides
slides.filter('.active').stop(true, false).fadeOut(speed, function(){
$(this).removeClass('active');
});
targetSlide.stop(true, false).fadeIn(speed).addClass('active');
tabs.removeClass('selected');
self.parent().addClass('selected');
});
非常感謝
我不明白你爲什麼需要這個自定義事件。你不能直接將匿名函數綁定到click事件嗎? – alextercete 2012-03-30 01:38:20