document.createElement("标签名"); //创建标签
document.body.appendChild(obj); //必须把创建的标签放到document中才有效
document.body; //获得body这个对象
appendChild; //添加子标签
obj.parentNode.removeChild(obj); //删除节点。必须通过父节点删除。
this:事件源,对象本身。this和event一样,可以当做参数传递。(this对象,event事件)
给对象绑定事件: obj.οnclick=函数名; //注意:函数名后面不加括号
document.body.appendChild(obj); //必须把创建的标签放到document中才有效
document.body; //获得body这个对象
appendChild; //添加子标签
obj.parentNode.removeChild(obj); //删除节点。必须通过父节点删除。
this:事件源,对象本身。this和event一样,可以当做参数传递。(this对象,event事件)
给对象绑定事件: obj.οnclick=函数名; //注意:函数名后面不加括号
在绑定事件函数中,this是可以直接用的。this.parentNode就是获得this的父节点
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META name="Generator" content="EditPlus">
<META name="Author" content="">
<META name="Keywords" content="">
<META name="Description" content="">
<style type="text/css">
*{margin:0}
#div{
margin:0 auto;
border:1px solid red;
width:950px;
height:700px;
position:relative;
}
</style>
<script type="text/javascript">
window.οnlοad=init;
function init(){
document.body.bgColor="black";
}
var dingshiqi;
function star(){
dingshiqi=window.setInterval("stars()",1000);
}
function stars(){
var parent=document.getElementById("div");
var img1=document.createElement("img");
img1.src="images/xingxing.gif"
var size=Math.floor(Math.random()*70+20);
img1.width=size;
img1.style.position="absolute";
var x=Math.floor(Math.random()*950);
var y=Math.floor(Math.random()*700);
img1.style.top=y+"px";
img1.style.left=x+"px";
parent.appendChild(img1);
img1.οnclick=removestar; //img1对象绑定事件。注意绑定事件时,后面函数不能加括号,this和event直接在函数中用就可以。
}
//删除节点,必须通过父节点删除节点
function removestar(){
this.parentNode.removeChild(this); //绑定事件函数中,可以直接用this表示绑定该事件的对象。
}
</script>
</HEAD>
<BODY>
<input type="button" value="开始" οnclick="star()"/>
<div id="div"></div><br />
</BODY>
</HTML>