1

我的页面网址是

http://somain.com/test.php?id=1&mview=5

登录后,上面的 url 显示选项名称和图像。我们必须单击添加新条目按钮来添加选择图像,而要删除,我们必须单击删除按钮。但是删除按钮和文件上传部分在 chrome 中工作正常,在 IE 8 中不工作。

我使用了一个 javascript 函数来添加图像以供选择。但是所有 js 函数在 chrome 中都可以正常工作,但在 IE 8 中无法正常工作。这是删除文件上传部分的简单编码

function addnewchoice(val)
{
var remove = document.createElement("input");
remove.setAttribute("type", "button");
remove.setAttribute("value", "Remove");
remove.setAttribute("onclick", "Remover("+ val +")");
remove.setAttribute("class", "removechoice");
remove.setAttribute("name", removename);
}

function Remover(idval)
{
document.forms['choiceadd']['imageshow'+idval].style.display="none";
document.forms['choiceadd']['choicefile'+idval].style.display="none";
document.getElementById('fileuploadover'+idval).style.display="none";
document.forms['choiceadd']['choicename'+idval].style.display="none";
document.forms['choiceadd']['choicename'+idval].value="";
document.forms['choiceadd']['remove'+idval].style.display="none";
document.getElementById('br'+idval).style.display="none";
}

任何人都请帮我解决这个问题。非常感谢您阅读并帮助我解决此问题

4

1 回答 1

3

根据这里的第一个答案——IE 不允许动态创建的 DOM 'a' 元素上的 onClick 事件——当元素的 onclick 属性动态更改时,IE8 不会自动绑定点击事件处理程序。也就是说,您在 HTML 中设置标签的 onclick 属性,但浏览器并未将该属性转换为实际的事件处理程序。

尝试更换

remove.setAttribute("onclick", "Remover("+ val +")");

var removeFunc = function() { Remover(val) };
if (!remove.addEventListener) //Old IE
  remove.attachEvent("onclick", removeFunc);
else //Other browsers
  remove.addEventListener("click", removeFunc );

这实际上直接绑定了 click 事件处理程序,而不是设置属性以期望浏览器会通过绑定来做出反应。

于 2012-12-03T11:39:05.737 回答