jQuery的点击()事件不触发对AJAX加载HTML元素 [英] jQuery click() event not firing on AJAX loaded HTML elements

查看:207
本文介绍了jQuery的点击()事件不触发对AJAX加载HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的主题是pretty的描述我的问题,我假设它不会以这种方式工作,有没有一种方法,使工作? (解决方法)?

下面是通过AJAX加载的code:

 < D​​IV>
< D​​IV ID =s0frame级=sframe>< / DIV>
< D​​IV ID =s1frame级=sframe>< / DIV>
< D​​IV ID =s2frame级=sframe>< / DIV>
< D​​IV ID =s3frame级=sframe>< / DIV>
< D​​IV ID =s4frame级=sframe>< / DIV>
< D​​IV ID =s5frame级=sframe>< / DIV>
< D​​IV ID =chatframe级=聊天alpha60>< / DIV>
< / DIV>
 

下面是我的单击事件:

  $('。sframe)。绑定(点击,函数(){
    VAR seat_number = this.id.match(/ \ D / G);
    警报(seat_number);
});
 

解决方案

做到这一点。

  $(文件)。在(点击,sframe功能(E){
 VAR seat_number = this.id.match(/ \ D / G);
 警报(seat_number);
 });
 

  $(文件).delegate(sframe,点击,功能(五){
 VAR seat_number = this.id.match(/ \ D / G);
 警报(seat_number);

 });
 

编辑:

在jQuery 1.7中,.live()方法去precated。使用。对()附加的事件处理程序。老版本的jQuery用户应使用.delegate()。

the subject is pretty descriptive of my problem, I am assuming it won't work this way, is there a way to make it work? (workaround)?

Here is the code that is loaded via 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>

Here is my click event:

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

解决方案

Do this.

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

or

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

 });

Edit:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate().

这篇关于jQuery的点击()事件不触发对AJAX加载HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆