最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - .remove is not a function - Stack Overflow

matteradmin4PV0评论

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.

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.

Share Improve this question asked Sep 21, 2014 at 3:34 ShellShell 6,84911 gold badges43 silver badges74 bronze badges 2
  • What's the value of els at the point of failure? – isherwood Commented Sep 21, 2014 at 3:36
  • Its a div at els[els.length - 1] element. – Shell Commented Sep 21, 2014 at 3:37
Add a ment  | 

1 Answer 1

Reset to default 6

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

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

or even better, this:

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

comment list (0)

  1. No comments so far