使用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