最近、動的に作成された 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");
});