1
我試圖動態地添加/刪除DOM元素(id =「result」)。添加似乎工作正常,但刪除後,該元素仍然出現在頁面上。刪除的DOM元素仍然出現在頁面上
這裏發生了什麼?我究竟做錯了什麼?
這裏是我的代碼:
<!DOCTYPE html>
<html>
<body>
<script>
function clearResult() {
if (document.getElementById("result") != null){
alert("remove #result");
$("#result").remove();
if (document.getElementById("result") != null) {
alert("#result still exists");
}
}
alert("Exiting clearResult");
}
function search() {
clearResult();
if (document.getElementById("result") == null) {
alert("add #result");
$('<table id="result"><tr>I am a table</tr></table>').appendTo('#ex');
}
}
</script>
<div>
<button id="search" onclick="search()" value="Send">Search</button>
</div>
<div id="ex">
@*Add result table here dynamically*@
</div>
</body>
</html>
TD在哪裏? ? –