动态创建的元素在iE9事件中不起作用

时间:2011-12-15 13:36:57

标签: javascript internet-explorer-9 setattribute createelement

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中没有。

1 个答案:

答案 0 :(得分:0)

你在setAttribute上使用eval,也许这就是为什么它不起作用。

但你可以用这样的东西来避免评估(这是一个很好的做法):

var ibutton = document.createElement('input');
ibutton.setAttribute('type', 'button');
ibutton.setAttribute('name', 'button');
ibutton.setAttribute('value', 'Delete');
ibutton.onclick = function() { deleteImage.apply(this, '<?php echo $_FILES['dt_file']['name'];?>', this,'<?php echo $image_type;?>');return false };`

如果你使用this将引用你的deleteImage函数中的ibutton。

相关问题