javascript .remove() 在 Internet Explorer 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32670780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.remove() not working in Internet Explorer
提问by Thiago
This code works very well in Google Chrome, but wont work in Internet Explorer:
此代码在 Google Chrome 中运行良好,但在 Internet Explorer 中不起作用:
document.getElementsByClassName('info')[i].remove();
Is there some other method to do the same thing or can I make .remove()
work in Internet Explorer?
有没有其他方法可以做同样的事情,或者我可以.remove()
在 Internet Explorer 中工作吗?
回答by Patrick Evans
You would have to get the parent and call removeChild
您必须获取父项并调用 removeChild
var node = document.getElementsByClassName('info')[i];
node.parentNode.removeChild(node);
Also since you have jQuery tagged you could just do
此外,因为你有 jQuery 标记你可以做
jQuery(".info").eq(i).remove()
as jQuery does cross browser checks and uses the correct methods
因为 jQuery 会跨浏览器检查并使用正确的方法