Javascript iE9事件中动态创建的元素不起作用 var ibutton=document.createElement('input'); setAttribute('type','button'); setAttribute('name','button'); setAttribute('value','Delete'); setAttribute('onclick','deleteImage('',this',);返回false;“”`

Javascript iE9事件中动态创建的元素不起作用 var ibutton=document.createElement('input'); setAttribute('type','button'); setAttribute('name','button'); setAttribute('value','Delete'); setAttribute('onclick','deleteImage('',this',);返回false;“”`,javascript,internet-explorer-9,setattribute,createelement,Javascript,Internet Explorer 9,Setattribute,Createelement,上面的代码创建了一个删除按钮。这仅适用于最新创建的按钮 当我单击并选择一个图像时,会使用此按钮动态创建图像,但每次单击并选择一个图像时,事件onclick仅适用于最新创建的按钮。以前创建的所有元素都没有工作事件,但不会显示任何错误 上述代码在Mozilla中运行良好,但在IE9中不起作用。您使用的是setAttribute上的eval,可能这就是它不起作用的原因 但您可以通过以下方式避免评估(这是一个很好的做法): var ibutton = document.createElement('in

上面的代码创建了一个删除按钮。这仅适用于最新创建的按钮

当我单击并选择一个图像时,会使用此按钮动态创建图像,但每次单击并选择一个图像时,事件
onclick
仅适用于最新创建的按钮。以前创建的所有元素都没有工作事件,但不会显示任何错误


上述代码在Mozilla中运行良好,但在IE9中不起作用。

您使用的是setAttribute上的eval,可能这就是它不起作用的原因

但您可以通过以下方式避免评估(这是一个很好的做法):

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;");`
var ibutton=document.createElement('input');
setAttribute('type','button');
setAttribute('name','button');
setAttribute('value','Delete');
ibutton.onclick=function(){deleteImage.apply(this',this',),返回false}`

如果您使用
,此
将引用deleteImage函数中的ibutton。

函数deleteImage已经存在于我包含的其他javascript文件中。但是这里在setAttribute中,deleteImage正在获取参数。编辑以应用deleteImage函数,使用您需要的范围和参数。
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 };`