document对象
最重要的三个方法
getElementById [html php jsp] (如果页面中有多个相同的id,则返回第一个对象引用)
getElementsByName 通过html控件的名字返回对象集合 多用于多选。
getElementsByTagName 通过html的标签名返回对象集合
案例:请选择你的爱好
<script type="text/javascript">
//通过id获取对象
function test()
{
var a1=document.getElementById("a1");
//如果要取出某个属性
window.alert(a1.id+" "+a1.href+" "+a1.innerText);
a1.innerText="连接到百度";
a1.href="http://www.baidu.com";
}
//通过name来获取.对象
function test2()
{
//使用
var hobbies=document.getElementsByName("hobby");
//遍历这个集合
for(var i=0;i<hobbies.length;i++)
{
//判断
//window.alert(i+ " "+hobbies[i].checked);
if(hobbies[i].checked)
{ window.alert("你的爱好是"+hobbies[i].value); }
}
}
//通过标签名
function test3()
{
var inputs=document.getElementsByTagName("input");
window.alert(inputs.length);
}
</script>
<body>
<a id="a1" href="http://www.sina.com">连接到sina</a><br/>
<a id="a1" href="http://www.sohu.com">连接到sohu</a><br/>
<a id="a3" href="http://www.baidu.com">连接到baidu</a><br/>
<input type="button" value="测试" οnclick="test()"/><br/>
请选择你的爱好:
<input type="checkbox" name="hobby" value="旅游">旅游 <input type="checkbox" name="hobby" value="音乐">音乐 <input type="checkbox" name="hobby" value="体育">体育 <input type="checkbox" name="hobby" value="电影">电影
<input type="button" value="看看你的爱好" οnclick="test2()"/><br/>
<input type="button" value="通过tagname来获取元素" οnclick="test3()"/><br/>
</body>
动态的创建元素(节点)/添加元素(节点)/删除元素(节点)
注意用四个节点属性:
createElement() 方法创建新的元素节点:
appendChild() 方法向已存在的节点添加子节点。(发射子弹)
removeChild() 方法删除指定的节点。 (子弹消失)
parentNode 属性可返回某节点的父节点。
<script type="text/javascript">
function test1(){
//window.alert('ok');
//1创建 a 元素 createElment(标签名),
var myhref=document.createElement("a"); //<a >???</a>
myhref.innerText="连接到sina";
myhref.href="http://www.baidu.com"
myhref.id="myhref";
//document.body.appendChild(myhref);
div1.appendChild(myhref);
}
function test2()
{
//document.getElementById('div1').removeChild(document.getElementById('myhref'));
var node=document.getElementById('myhref');
node.parentNode.removeChild(node);
}
</script>
<body>
<input type="button" value="创建一个a标签" οnclick="test1()"/><br/>
<input type="button" value="删除a标签" οnclick="test2()"/><br/>
<!--引入 css id class [dom如何去操作一个外表css!补讲 ]-->
<div id="div1" style="width:200px;height:200px;background-color:green">div1</div>
</body>
制作一个函数查询按键的keycode编码! 代码如下:
<script type="text/javascript">
function test()
{
window.alert("我所按下的键的keycode的代码是"+window.event.keyCode);
}
</script> <body>
<input type="button" οnkeydοwn="test()" value="tesing"/>
</body>
乌龟发子弹的练习
(1) 响应用户按 j 键
(2) 让乌龟发子弹 函数 fire内容多.
创建子弹
我认为子弹就是一个图片 / 也可以认为是一个div span
怎样让子弹碰到边界后,就停下,并且消失
提示: window.alert("body的宽="+document.body.clientWidth);
<script type="text/javascript">
//定义两个类
function Tortoise(x,y)
{
this.x=x;
this.y=y;
//var bullet_img="";//子弹
//子弹数组
var bullet_imgs=new Array();
var bullet_img_speed=2;
this.speed=1;//移动一个像素
var isStart=false;
this.move_up=function(){
this.y-=this.speed;
//同时修改乌龟的top值.
//dom编程体现
//1先得到这个乌龟图片 div
var wugui_div=document.getElementById("wugui");
wugui_div.style.top=this.y+"px";
//判断乌龟是否和公鸡碰撞.
//window.alert("乌龟当前的 x y"+this.x+" "+this.y);
//window.alert("公鸡当前的 x y"+cock.x+" "+cock.y);
}
//向右移动
this.move_right=function()
{
this.x+=this.speed;
//同时修改乌龟的top值.
//dom编程体现
//1先得到这个乌龟图片 div
var wugui_div=document.getElementById("wugui");
wugui_div.style.left=this.x+"px";
}
//开火
this.fire=function(){
//window.alert("乌龟发子弹");
var bullet_img=document.createElement("img");
bullet_img.src="bullet.bmp";
//设置bullet_img的x,y
//定子弹的横坐标
bullet_img.style.left=(this.x+94)+"px";
bullet_img.style.top=(this.y+33)+"px";
//做出绝对定位
bullet_img.style.position="absolute";
document.body.appendChild(bullet_img);
//把这颗新子弹放入 数组中,进行管理
bullet_imgs[bullet_imgs.length]=bullet_img;
//启动子弹[请注意一下,如果我们调用是一个对象的成员方法]
//setInteval 应该这样去掉 window.setInteval("对象名.函数名()",时间);
//如果子弹数组中有子弹,就不要在调用
/* if(bullet_imgs.length==1){
window.setInterval("tortoise.move_bullet()",50);
} */
if(!isStart)
{
window.setInterval("tortoise.move_bullet()",10);
isStart=true;
}
}
//让子弹移动
this.move_bullet=function(){
//遍历子弹数组.看看每颗子弹是否已经到到边界,如果到了边界,则删除
//for(var i=0;i<bullet_imgs.length;i++){
//}
if(bullet_imgs.length==0){
//stop定时器.
//isStart=false;
}
//这里要求访问 bullet_img
//修改bullet_img 的 left即可 100px
//遍历子弹数组,让每颗子弹移动
for(var i=0;i<bullet_imgs.length;i++){
//取出每颗子弹
var bullet_img=bullet_imgs[i];
var
bullet_img_x=bullet_img.style.left.substring(0,bullet_img.style.left.indexOf("p"));
bullet_img.style.left=parseInt(bullet_img_x)+bullet_img_speed+"px";
//遍历子弹数组.看看每颗子弹是否已经到到边界,如果到了边界,则删除
if(parseInt(bullet_img_x)+17>=document.body.clientWidth){
//到达边界,就删除该子弹
bullet_imgs.splice(i,1); //3 2 1
//同时从html dom树种删除
bullet_img.parentNode.removeChild(bullet_img);
}
} }
}
function Cock(x,y){
this.x=x;
this.y=y;
this.speed=1;//一定一个像素 setInterval(定时器)
}
//创建全局的乌龟对象和公鸡对象
var tortoise=new Tortoise(100,120);
var cock=new Cock(200,200);
//用于响应用户点击的操作
function move(obj){
switch(obj.value){
case "向上走":
tortoise.move_up();
break;
case "向右走":
tortoise.move_right();
break;
}
}
//用于响应用户按键的操作
function move2(event){
switch(event.keyCode){
case 65:
tortoise.move_left(); //乌龟向左
break;
case 68:
tortoise.move_right();
break;
case 83:
tortoise.move_down();
break;
case 87:
tortoise.move_up();
break;
//响应用户按j这个键
case 74:
//发送子弹的行为封装到fire
tortoise.fire();
break;
default:
break;
}
}
</script>
head>
<body οnkeydοwn="move2(event)">
<table id="mytable" border="1">
<tr>
<td></td>
<td><input type="button" value="向上走" οnclick="move(this)" /></td> <td>shunping</td>
</tr>
<tr>
<td><input type="button" value="向左走" /></td> <td></td>
<td><input type="button" value="向右走" οnclick="move(this)" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="向下走" /></td> <td></td>
</tr>
</table>
<!--把乌龟放在一个div-->
<div id="wugui" style="position: absolute left:100px;top:120px;">
<img src="1.bmp" border="1" alt="" />
</div>
<div id="cock" style="left:200px;position:absolute;top:200px;">
<img src="2.bmp" border="1" alt="" />
</div>
</body> </html>
常用的dom 的每个Node 节点属性和方法(加强篇)
(取到父节点、兄弟节点、子节点等 )
<head>
<title>无标题页</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head> <body>
<table border="1">
<tr>
<td></td>
<td><input type="button" value="向上走" οnclick="move(this)" /></td> <td></td>
</tr>
<tr>
<td><input type="button" value="向左走" οnclick="move(this)" /></td> <td></td>
<td><input type="button" value="向右走" οnclick="move(this)" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="向下走" οnclick="move(this)" /></td> <td></td>
</tr>
</table>
<div id="wugui" style="position: absolute left:100px;top:120px;">
<img src="1.bmp" border="1" alt="" />
</div>
<div id="cock" style="left:200px;position:absolute;top:200px;">
<img src="2.bmp" border="1" alt="" />
</div>
<input type="button" value="tesing" οnclick="test()"/>
<input type="button" value="tesing2" οnclick="test1()"/>
</body>
<script type="text/javascript">
function test()
{
//得到乌龟节点
var wugui=document.getElementById("wugui");
window.alert("类型"+wugui.nodeType);
window.alert("名字"+wugui.nodeName);
var wugui_parent=wugui.parentNode;
window.alert("父节点类型"+wugui_parent.nodeType);
window.alert("父节点名字"+wugui_parent.nodeName);
var wugui_next_borther=wugui.nextSibling;
window.alert("下一个兄弟 类型"+wugui_next_borther.nodeType);
window.alert("下一个兄弟 名字"+wugui_next_borther.nodeName);
var wugui_pre_borther=wugui.previousSibling;
//取出孩子节点 var wugui_chlid=wugui.firstChild;
window.alert("孩子 类型"+wugui_chlid.nodeType);
window.alert("孩子 名字"+wugui_chlid.nodeName+wugui_chlid.src); }
function test1()
{
document.bgColor="black";//背景色
document.fgColor="white";//前景色
window.alert(document.title+" "+document.URL);
}
</script> </html>