1

この関数は一度だけ機能し、アンカー要素をもう一度クリックしても何も起こりません。セレクターは、一致したすべての要素に .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');


}
4

3 に答える 3

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');
});
于 2012-06-26T09:24:01.660 に答える
0

私はそれをこのようにバインドします:

$('#welcome li').on('click', 'a', function(e) {
    e.preventDefault();
    whatever();
});
于 2012-06-26T09:45:45.823 に答える
0

単純な「return false;」を試すことができます。.click イベント関数でやりたいことをすべて実行した後。

于 2012-06-26T09:36:34.887 に答える