在我的以下代码中,我使用 java 脚本创建了一个 div。当我单击按钮时,div会重复显示。我应该怎么做才能显示一次并被清除。
<html>
<head>
<title>Div tag using Javascript
</title>
<!--<link href="divtag1.js" type="text/javascript">-->
<style>
body{
background:#00ffff;
}
.myclass{
background:#30a0d3;
color:#123456;
position:relative;
left:300px;
right:300px;
top:150px;
height:100px;
width:700px;
border:2px solid #000000;
border radius:5px;
padding:20px;
margin:50px;
}
</style>
<script>
function myfunction(){
var div = document.createElement('div');
div.innerHTML = "my <b>new</b> skill - <large>DOM maniuplation!</large>";
// set style
div.style.color = 'red';
// better to use CSS though - just set class
div.setAttribute('class', 'myclass'); // and make sure myclass has some styles in css
document.body.appendChild(div);
var oldChild=element.removeChild(child)
element.removeChild(child);
}
</script>
</head>
<body>
<div>
<input type="button" value="Click Me!!" onclick="return myfunction();"></div>
</body>
</html>
任何建议都受到高度赞赏。