日期:2014-05-20  浏览次数:20728 次

问个JavaScript 问题?
我要在top中添加img,可以
var   div1=document.getElementById( "top ");
var   img=document.createElement( "image ");
img.src= "./Images/luobinmao.jpg ";
div1.appendChild(img);
问题是此处动态添加的img在html中是没有标记的,此时怎么删除?
用removeChild不行!

------解决方案--------------------

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> 无标题页 </title>
<script type= "text/javascript ">
window.onload=function(){
var div1=document.getElementById( "div1 ");
var img=document.createElement( "image ");
img.setAttribute( "id ", "image1 ");//设置id为image1
img.src= "../1.jpg ";
div1.appendChild(img);

}

function remove(){
var div1=document.getElementById( "div1 ");
var img=document.getElementById( "image1 ");
div1.removeChild(img);//通过id找到image并删除

}
</script>
</head>
<body>
<div id= "div1 ">
<input id= "Button1 " type= "button " value= "button " onclick= "remove() " />
</div>
</body>
</html>

------解决方案--------------------
img=null;
div1.innerHTML = " ";

OR

img.style.display = "none ";
------解决方案--------------------
看过,不记得了........