简体   繁体   English

javascript函数在chrome中工作但在IE8中不起作用

[英]javascript function working in chrome but not working in IE8

My page url is 我的页面网址是

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

After login the above url shows choice names and images. 登录后,上面的url显示选择名称和图像。 We have to click add new entry button to add choice images and for remove just we have to click remove button. 我们必须单击添加新的条目按钮来添加选择图像,并且要删除我们必须单击删除按钮。 But the remove button and the file upload section is working fine in chrome and not working in IE 8. 但删除按钮和文件上传部分在Chrome中工作正常,在IE 8中无法正常工作。

I used a javascript function for adding images for choices. 我使用javascript函数添加图像供选择。 But all the js functions working fine in chrome but it is not working in IE 8. This is the simple coding for removing the file upload section 但是所有的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";
}

Anyone please help me to solve this problem. 有人请帮我解决这个问题。 Thank you so much for reading and help me to resolve this problem 非常感谢您的阅读并帮助我解决这个问题

According to the first answer here -- IE not allowing onClick event on dynamically created DOM 'a' element -- IE8 does not bind click event handlers automatically when the onclick attribute of an element is changed dynamically. 根据这里的第一个答案 - IE不允许onClick事件动态创建DOM'a'元素 - 当动态更改元素的onclick属性时,IE8不会自动绑定click事件处理程序。 That is, you're setting the onclick attribute of your tag in HTML, but the browser isn't converting that attribute into an actual event handler. 也就是说,您要在HTML中设置标记的onclick属性,但浏览器不会将该属性转换为实际的事件处理程序。

Try replacing 尝试更换

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

with

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

That actually binds the click event handler directly, rather than setting the attribute in the expectation that the browser will react by binding it. 这实际上直接绑定了click事件处理程序,而不是设置属性,期望浏览器通过绑定它来做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM