jQuery click() イベントが AJAX で読み込まれた HTML 要素で発生しない 質問する

jQuery click() イベントが AJAX で読み込まれた HTML 要素で発生しない 質問する

件名は私の問題をかなりよく表しています。この方法ではうまくいかないと思いますが、うまく機能させる方法はありますか? (回避策)?

AJAX 経由で読み込まれるコードは次のとおりです。

<div>
<div id="s0frame" class="sframe"></div>
<div id="s1frame" class="sframe"></div>
<div id="s2frame" class="sframe"></div>
<div id="s3frame" class="sframe"></div>
<div id="s4frame" class="sframe"></div>
<div id="s5frame" class="sframe"></div>
<div id="chatframe" class="chat alpha60"></div>
</div>

クリック イベントは次のとおりです。

$('.sframe').bind('click', function() { 
    var seat_number = this.id.match(/\d/g);
    alert(seat_number); 
});

ベストアンサー1

これを行う。

 $(document).on("click",".sframe",function(e){
 var seat_number = this.id.match(/\d/g);
 alert(seat_number); 
 });

または

 $(document).delegate(".sframe","click",function(e){
 var seat_number = this.id.match(/\d/g);
 alert(seat_number); 

 });

編集:

jQuery 1.7 以降、.live() メソッドは非推奨です。イベント ハンドラーをアタッチするには、.on() を使用してください。古いバージョンの jQuery のユーザーは、.delegate() を使用する必要があります。

おすすめ記事