使用jquery为动态创建的特定元素添加事件侦听器 [英] Add Event Listener for dynamically created particular element using jquery

查看:71
本文介绍了使用jquery为动态创建的特定元素添加事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery创建动态元素,并尝试绑定元素的侦听器。
通过绑定侦听器使用

I'm creating dynamic elements using jquery and try to bind listener for elements. by binding listener using

$('#div11').on('click',$('#textbox31'),function(e){showPopupImage(e);});
$('#div11').on('click',$('#textbox32'),function(e){alert('hi');});

函数,即使我指定要绑定的特定字段,它也会向后代添加监听器。

function,it adds listener to descendents even though i specify the particular field to bind.

pls,建议我处理我的情况的好方法。

pls,suggest me the good way to handle my situation.

推荐答案

问题是你没有为函数上的指定正确的参数。如果查看JQuery文档,则使用以下函数重载:

The problem is that you are not specifying the correct parameters to the on function. If you look at the JQuery documentation, you are using the following function overload:


.on(events [,selector] [,data] )

.on( events [, selector ] [, data ] )

您指定的部分是选择器参数。这个参数需要一个选择器字符串,而不是一个JQuery对象。

The part you are specifying incorrectly is the selector parameter. This parameter requires a selector string, not a JQuery objct.

以下内容可行:

$('#div11').on('click','#textbox31',function(e){showPopupImage(e);});
$('#div11').on('click','#textbox32',function(e){alert('hi');});

注意我已经更换了 $('#textbox31') with '#textbox31'

Notice I have replaced $('#textbox31') with '#textbox31'.

这是一个工作示例,您将看到点击事件未应用于 textbox33 textbox34

Here is a working example, you will see that the click event is not applied to textbox33 and textbox34

这篇关于使用jquery为动态创建的特定元素添加事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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