此功能仅适用一次,当我再次单击一个锚元素时,不会发生。我以为选择器会将.click函数应用于所有匹配的元素?
$('#welcome-nav li a').click(function (e) {
// prevent anchor from firing
e.preventDefault();
var chosenElement = $(this).parent().attr('class');
var index = articles.indexOf('.' + chosenElement) + 1;
//remove all classes of active on articles
$.each(articles, function (index, value) {
$(value).removeClass('active');
})
$('.' + chosenElement).addClass('active');
$('#sharpContainer').bgStretcher.sliderDestroy();
startBgStretcher(returnImageArray(index));
})
下面是我认为打破onclick功能的插件
$('#sharpContainer').bgStretcher({
images: imageContainer,
anchoring: 'left top', //Anchoring bgStrtcher area regarding window
anchoringImg: 'left top', //Anchoring images regarding window
nextSlideDelay: 8000, //Numeric value in milliseconds. The parameter sets delay until next slide should start.
slideShowSpeed: 2000, //Numeric value in milliseconds or(’fast’, ‘normal’, ’slow’). The parameter sets the speed of transition between images
transitionEffect: 'superSlide',
slideDirection: 'W',
callbackfunction: homepageSlide
});
function homepageSlide() {
//homepage slide is called after a slide has loaded
var index = $('li.bgs-current').index();
//hide current article
$.each(articles, function (index, value) {
$(value).removeClass('active');
})
//show next article
$(articles[index]).addClass('active');
}
答案 0 :(得分:4)
我认为这可能是由于JQuery默认绑定的方式。如果您使用:
$(document).on('click', '#welcome-nav li a', function(e){
e.preventDefault();
alert('here');
});
会发生什么?
静态元素用法示例
$('#welcome-nav li').on('click', 'a', function(e){
e.preventDefault();
alert('here');
});
答案 1 :(得分:0)
你可以尝试简单的“返回假”;完成.click事件函数中要执行的所有操作之后。
答案 2 :(得分:0)
我会像这样绑定它:
$('#welcome li').on('click', 'a', function(e) {
e.preventDefault();
whatever();
});