<分区>
我正在尝试动态添加/删除 DOM 元素 (id ="result")。添加似乎工作正常,但删除后元素仍然出现在页面上。
这里发生了什么?我做错了什么?
这是我的代码:
<!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>