2

クリックすると ajax 呼び出しが行われ、一部の html が div にロードされてからスクリプトがロードされるアンカーがあります。最初のクリック後にそのイベントのバインドを解除し、その div の表示と非表示を切り替える別のクリック イベントに置き換えたいと考えています。私はここまでやってきましたが、div 作業を切り替えるクリック イベントを登録しようとする試みはありません。

$('#anchor').click( function() {
    $('#div').load('file.html', function() {    
        $.getScript('script.js');       
        $('#anchor').unbind();
    });
});
4

2 に答える 2

2
$('#anchor').click( function(e) {
    e.preventDefault(); // prevent the browsers following the link
    $('#div').load('file.html', function() {    

        // make sure to bind once the new script has been included
        // i.e. within .getScript's callback
        $.getScript('script.js', function() {
            $('#anchor').unbind("click").click(myShowHideFunc);
        });       
    });
});
于 2011-08-07T19:02:07.130 に答える
1

現在のイベント ハンドラーのみをバインド解除して、バインド解除時に他のクリック ハンドラーが影響を受けないようにするのに役立つイベント名前空間でこれを試してください。

$('#anchor').click('click.anchor', function() {

    $('#div').load('file.html', function() {    
        $.getScript('script.js', function(){       
          $('#anchor').unbind('click.anchor').click(function(){
             $('#div').toggle();
          });
        });
    });
});
于 2011-08-07T19:13:58.460 に答える