最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript document.getElementById().innerHTML = "" not working - Stack Overflow

programmeradmin5浏览0评论

I'm looking to clear the content within a <div> but if I run the code it doesn't work. New to JS and would love a hand from someone who knows how it works!

Code as is:

<html>
<head>
    <!-- Javascript -->
    <script type="text/javascript">

        //UpdatesPlayerRoster
        function refreshRoster(){
            document.getElementById('roster').innerHTML = "";
        }
    </script>
</head>
<body>

    <div id=bodyContainer class="bodyContainer">
    <div id=playerListContainer class="playerListContainer">
        <div id=playerListHeader class="playerListHeader"><span class="playerListHeaderText">Players</span></div>
        <div id=playerListBody class="playerListBody">
        <table>
        <div id=roster>
            <tr><td>CONTENT TO BE CLEARED</td></tr>
        </div>
        </table>
    </div>

    <a href="#" onClick = "refreshRoster()">Refresh</a>
    </div>

</body>

I'm looking to clear the content within a <div> but if I run the code it doesn't work. New to JS and would love a hand from someone who knows how it works!

Code as is:

<html>
<head>
    <!-- Javascript -->
    <script type="text/javascript">

        //UpdatesPlayerRoster
        function refreshRoster(){
            document.getElementById('roster').innerHTML = "";
        }
    </script>
</head>
<body>

    <div id=bodyContainer class="bodyContainer">
    <div id=playerListContainer class="playerListContainer">
        <div id=playerListHeader class="playerListHeader"><span class="playerListHeaderText">Players</span></div>
        <div id=playerListBody class="playerListBody">
        <table>
        <div id=roster>
            <tr><td>CONTENT TO BE CLEARED</td></tr>
        </div>
        </table>
    </div>

    <a href="#" onClick = "refreshRoster()">Refresh</a>
    </div>

</body>

Share Improve this question asked Oct 25, 2015 at 14:24 seanbulleyseanbulley 151 silver badge5 bronze badges 1
  • <div id=playerListHeader — HTML provides a collection of heading elements (<h1> - <h6>). – Quentin Commented Oct 25, 2015 at 14:29
Add a ment  | 

2 Answers 2

Reset to default 7

Your HTML is invalid. Use a validator.

You can't have a <div> element as a child element of a <table>.

Your browser is, most likely, performing error recovery by moving the <div> so it appears after the table and leaving the <tr> behind. It doesn't have any content to start with, so when you empty it with JS, it makes no difference.

Use a <tbody> instead of a <div>.

Here, try this:

<html>
<head>
    <!-- Javascript -->
    <script type="text/javascript">

        //UpdatesPlayerRoster
        function refreshRoster(){
            document.getElementById('roster').innerHTML = "";
        }
    </script>
</head>
<body>

    <div id="bodyContainer" class="bodyContainer">
    <div id="playerListContainer" class="playerListContainer">
        <div id="playerListHeader" class="playerListHeader"><span class="playerListHeaderText">Players</span></div>
        <div id="playerListBody" class="playerListBody">

            <div id="roster">
                <table>
                    <tr><td>CONTENT TO BE CLEARED</td></tr>
                </table>
            </div>
        </div>

        <a href="#" onClick = "refreshRoster()">Refresh</a>
    </div>

</body>

You need the <table> element to be inside of the div for it to work properly as browsers will correct this by itself. If you look at the page through inspect element, you'll see the browser has moved the table out of the div, which is why your code did not clear anything.

Alternatively, as the other answer states, you can use <tbody> instead of the <div>.

发布评论

评论列表(0)

  1. 暂无评论