[英]dynmaic anchor onclick event is not triggered
i am trying to achieve the same behavior in this fiddle http://jsfiddle.net/CWaHL/1/ but the only difference is my anchor tag is dynamically generated through jquery 我试图在这个小提琴http://jsfiddle.net/CWaHL/1/中实现相同的行为,但唯一的区别是我的锚标签是通过jquery动态生成的
my code 我的代码
acqIdCounter = grouparr.length;
$("#attendessbox") .append( "
<div id='acquiantancebox"+acqIdCounter+"' class='acquaintance'
style='padding: 1%; float: left; position: relative;'>
<a href='#' class='deleteAcq' onClick='return false;'>
<img style='max-width: 100%' src='images/crosserror.png' /></a><span
style='border: 1px solid #DE4062; width: 100%; border-radius: 4px'>"
+ acquiantancename + "<br />" + acquiantancemob + "
</span>
</div>
and on click event 以及点击事件
$('a.deleteAcq').on('click',function(e){
alert("test");
return false;
});
so issue here, when click on the image 'crosserror.png' my anchor click event is not getting triggered. 所以问这里,当点击图片'crosserror.png'时,我的锚点击事件没有被触发。 Thanks for your answers, dont know what i am doing wrong!! 谢谢你的回答,不知道我做错了什么!!
The event delegation syntax of on
is different. on
的事件委托语法不同。
$(static-eleemnt).on(event, dynamic-element-selector, function(){})
Try 尝试
$(document).on('click','a.deleteAcq',function(e){
alert("test");
return false;
});
You need to delegate the event to the parent, so you catch it when it bubbles up from the dynamically created elements. 您需要将事件委托给父级,因此当它从动态创建的元素中冒出时会捕获它。
$('#attendessbox').on('click', 'a.deleteAcq', function(e){
// Your code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.