admin管理员组文章数量:1405328
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
2 Answers
Reset to default 7Your 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>
.
本文标签: htmlJavaScript documentgetElementById()innerHTMLquotquot not workingStack Overflow
版权声明:本文标题:html - JavaScript document.getElementById().innerHTML = "" not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744868990a2629513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论