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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:35:33  来源:igfitidea点击:

.remove() not working in Internet Explorer

javascriptjquerycross-browser

提问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

remove is not supported by ie

ie 不支持删除

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 会跨浏览器检查并使用正确的方法