我有很多具有不同 ID 的动态文本框。我试图在点击时删除它们我有一个功能
<script>
function removing(id){
alert(id);
id.remove();
}
</script>
和我的 html 文本框
<input type="text" id="a" name="name" onclick="removing(this.id)" />
当我点击它时。它提供警告框但不删除文本框有任何帮助吗?
最佳答案
您需要元素本身,所以只需传递 this
<input type="text" id="a" name="name" onclick="removing(this)" />
然后函数就可以了
function removing(elm){
alert(elm.id);
elm.parentNode.removeChild(elm);
}
您需要使用 removeChild
from parentNode
将其从 DOM 中移除。
关于javascript - 如何在 javascript 中使用动态 id 删除文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145543/