javascript/html:在按钮点击时删除元素的ID

javascript/html: Remove element by ID on button click

本文关键字:删除 元素 ID html 按钮 javascript      更新时间:2023-09-26

总之,我想做的是使它,所以当我点击一个按钮在我的HTML文档,我希望该按钮随后被它的ID删除,如果可能的话,使用javascript。我一直在寻找一个解决方案,但没有成功,有人能帮我吗?

在你的HTML中:

<button onclick="deleteSelf(this)">Click me</button>

在你的JavaScript中:

function deleteSelf(button) {
    button.remove();
}

这行得通:

https://jsfiddle.net/00p4oLfy/

document.getElementById('test').remove();
<p id="test">This is a test</p>
<p id="test2">This is another test</p>

Html

<div>
   <button id="ClickMe">Click Me</button>
</div>
Javascript

document.getElementById('ClickMe').remove();