javascript .remove 不是一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25955540/
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 05:18:01  来源:igfitidea点击:

.remove is not a function

javascriptjqueryasp.net-mvc

提问by Shell

This question may be duplicate of any. But, after lot of try I couldn't find the proper solution for this.

此问题可能与任何问题重复。但是,经过多次尝试,我找不到合适的解决方案。

This is my code

这是我的代码

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script>
    $(document).ready(function () {
        var els = document.querySelectorAll('body > *');
        els[els.length - 1].remove(); //getting error here            
    })
</script>

I don't know why my application is showing error in browser console like

我不知道为什么我的应用程序在浏览器控制台中显示错误,例如

TypeError: els[els.length - 1].remove() is not a function

When i can run same function in browser console window and it works. but, when i place my code in the page it shows me error like above. I have also tried to call .removeNode(boo)method but, it was also not working. Actually when i try to write ele[].remove()in the code the intellisence doesn't suggest me that function.

当我可以在浏览器控制台窗口中运行相同的功能时,它就可以工作了。但是,当我将代码放在页面中时,它会显示我上面的错误。我也试过调用.removeNode(boo)方法,但它也不起作用。实际上,当我尝试编写ele[].remove()代码时,智能并不建议我使用该功能。

回答by JLRishe

DOMNodes don't have a remove()method. Use this:

DOMNodes没有remove()方法。用这个:

$(document).ready(function () {
    var els = document.querySelectorAll('body > *');
    $(els[els.length - 1]).remove(); 
});

or even better, this:

甚至更好的是:

$(document).ready(function () {
    $('body > *').last().remove();
});