1
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 中不行。

4

1 回答 1

0

您在 setAttribute 上使用 eval,也许这就是它不起作用的原因。

但是您可以通过以下方式避免 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 。

于 2011-12-15T13:44:52.430 回答