0

最近、動的に作成された div のトリガーについて質問したところ、.on() 関数が紹介されました。

「キーアップ」はうまく機能し、「マウスオーバー」は機能し、私がテストした他のいくつかは機能しますが、「クリック」は起動しません。

次のようなデータを保持する ajax と .php を介して div に追加情報を作成しました。

function loadNewcomment(divID) {
$.ajax({
        url: templateUrl+"/enternote.php",
        cache: false,
        success: function(html){
            $("#"+divID).append(html);

        }
    });
}

その作成された div 内の要素のキーアップでトリガーしたかったのですが、このコードはそのために機能します。

$("#notebox").on('keyup', '.note-field', function(event){
    var thisString = $(this).val();
    $keyLength = thisString.length;
    $rowCount = Math.ceil($keyLength/40);
    $currentRow = $(this).attr("rows");

    if($currentRow < $rowCount){
        $(this).animate({rows:$rowCount},50);
    }        

});

ただし、このコードは機能しません。

$("#notebox").on('click', '.note-field', function() {
    alert("clicked");
});
4

4 に答える 4

0

.stopPropagation(); がありました。包括的な div のコードの別の場所で、今のところコメントアウトしましたが、うまくいきました。

于 2013-06-07T20:30:22.677 に答える
0

動的 DOM と jQuery は面倒です。非推奨であることは知っていますが、過去に使用して素晴らしい結果を得ました: http://api.jquery.com/live/

FWIW - bind() を使用することもできます - http://api.jquery.com/bind/

于 2013-06-07T20:24:43.817 に答える
0

HTMLが追加された後にクリックイベントを添付することは可能ですか?

function loadNewcomment(divID) {
$.ajax({
        url: templateUrl+"/enternote.php",
        cache: false,
        success: function(html){
            $("#"+divID).append(html).bind('click', function() { /* Click event code here */ }); // id divID is #notebox

        }
    });
}
于 2013-06-07T20:31:37.597 に答える
-1

次のようにクリックを使用できます。

$("#notebox").click( function() {
  alert("clicked");
});

http://jsfiddle.net/7MBw4/

于 2013-06-07T20:18:57.033 に答える