This article mainly introduces how to dynamically create and remove a div using js, and involves javascript dynamic operations on page elements, for more information about how to dynamically create and remove p in js, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
// Create pfunction createDiv () {if (document. getElementByIdx_x ("newD ")! = Null) document. body. removeChild (document. getElementByIdx_x ("newD"); var Div = document. create_rElement ("p"); // Create a p Div. setAttribute ("id", "newD"); Div. style. position = "absolute"; Div. style. height = "160px"; Div. style. width = "400px"; Div. style. border = "1px solid gray"; Div. style. background = "white"; Div. style. paddingTop = "40px"; Div. style. zIndex = "100"; Div. style. left = 100 + "px"; // note that "px" Div must be added under Firefox. style. top = 100 + "px"; Div. style. textAlign = "center"; Div. style. fontSize = "18px"; var str ="<\/P> "; Div. innerHTML = str + title; document. body. appendChild (newD);} // remove the created pfunction removeDiv () {document. body. removeChild (document. getElementByIdx_x_x ("newD "));}
I hope this article will help you design javascript programs.