我正在尝试为我的图像创建onclick事件,但它的not workin..the警报没有显示。
$(document).ready(function(){
$('img.closeAttrIcon').click(function(){
alert("ww");
});
});即使我试着对..bind这样做..没有希望..。
$(document).ready(function(){
$('img.closeAttrIcon').bind('click',function(){
alert("ww");
});
});html将在此事件期间创建:
$('a#btnAddStdntAttr').click(function(){
var newAttr = '<div class="attrParent"><br class="clear"><label></label><input type="text" id="stdntAttributeKey1" maxlength="250" style="width: 13%;" name="stdntAttributeKey1"/>';
newAttr+= '<input type="text" id="stdntAttributeValue1" maxlength="250" style="width: 13%;" name="stdntAttributeValue1"/>';
newAttr+= '<img src="<c:url value="/static/images/closeIcon.png"/>" onclick="removeStdntAttr();" class="closeAttrIcon" alt="Close" title="Close" /></div>';
$(this).after(newAttr);
});我做错了什么? here..please帮助..
发布于 2012-05-01 18:58:05
它不起作用,因为您是在首次创建DOM之后注入它的,因此需要使用live (如果使用til jQuery 1.7)或on (如果使用(1.7+)绑定方法
将您的呼叫更改为:
$(function(){
$('img.closeAttrIcon').live('click', function() {
alert("ww");
});
});如果你使用的是jQuery 1.7+,那么你也可以这样做:
$(document).on('click', 'img.closeAttrIcon', function() {
alert("ww");
}); https://stackoverflow.com/questions/10381176
复制相似问题