javascript method to delete an html element node

  • 2020-05-07 19:14:52
  • OfStack

This article illustrates how to delete a specified html element with the native javascript implementation. Share with you for your reference. The specific implementation method is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="//www.ofstack.com/" />
<title> The home of the script </title>
<style>
#content{
  width:200px;
  height:20px;
  color:red;
}
</style>
<script>
window.onload=function(){
  var obt=document.getElementById("bt");
  var odiv=document.getElementById("content");
  obt.onclick=function(){
    odiv.parentNode.removeChild(odiv);
  }
}
</script>
</head>
<body>
<div id="content"> The home of the script </div>
<input type="button" id="bt" value=" See the effect "/>
</body>
</html>

In the native javascript, temporarily there is no one method to delete the element itself, if you want to delete can be through the parent node removeChild() method to delete the specified child elements, the code is relatively simple, here is not introduced, interested friends can see the relevant information.

I hope this article is helpful for you to design web programs based on javascript.


Related articles: