iE9 事件中动态创建的元素不起作用
var ibutton = document.createElement('input');
ibutton.setAttribute('type', 'button');
ibutton.setAttribute('name', 'button');
ibutton.setAttribute('value', 'Delete');
ibutton.setAttribute('onclick', "deleteImage('<?php echo $_FILES['dt_file']['name'];?>', this,'<?php echo $image_type;?>');return false;");`
上面的代码创建了一个删除按钮。这仅适用于最新创建的按钮。
当我单击并选择图像时,将使用此按钮动态创建图像,但每次我单击并选择图像时,事件 onclick
仅适用于最新创建的按钮。之前创建的所有元素都没有工作事件,但不显示任何错误。
上面的代码在 Mozilla 中工作正常,但在 IE9 中不行。
var ibutton = document.createElement('input');
ibutton.setAttribute('type', 'button');
ibutton.setAttribute('name', 'button');
ibutton.setAttribute('value', 'Delete');
ibutton.setAttribute('onclick', "deleteImage('<?php echo $_FILES['dt_file']['name'];?>', this,'<?php echo $image_type;?>');return false;");`
The above code creates a delete button. This works fine only for the latest created button.
When I click and select an image, the image is created dynamically with this button, but every time I click and select an image the event onclick
only works for the latest created button. All the element created before have no working events, but it does not display any errors.
The above code works fine in Mozilla, but not in IE9.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在 setAttribute 上使用 eval,也许这就是它不起作用的原因。
但是您可以通过以下方式避免 eval (这是一个很好的做法):
如果您使用
this
将引用您的 deleteImage 函数中的 ibutton 。You are using eval on setAttribute, maybe thats why it's not working.
But you can avoid eval (and it's a good practice to do that) with something like this:
if you use
this
will refer to ibutton inside your deleteImage function.