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
.remove is not a function
提问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
DOMNode
s don't have a remove()
method. Use this:
DOMNode
s没有remove()
方法。用这个:
$(document).ready(function () {
var els = document.querySelectorAll('body > *');
$(els[els.length - 1]).remove();
});
or even better, this:
甚至更好的是:
$(document).ready(function () {
$('body > *').last().remove();
});