1,event鼠标事件的例子
代码如下:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document 对象属性-www.jb200.com</title>
<script type="text/javascript">
//disabled 属性可设置或返回是否禁用按钮
//光标的坐标
function show_coords(event)
{
x=event.clientX;
y=event.clientY;
alert("X 坐标: " + x + ", Y 坐标: " + y)
}
//获取键盘的unicode
function whichbutton(event)
{
alert(event.keyCode);
}
function coordinates(event)
{
x=event.screenX;
y=event.screenY;
alert("相对于屏幕的坐标是:"+x+","+y);
}
function isKeyPressed(event)
{
if (event.shiftKey==1)
{
alert("The shift key was pressed!")
}
else
{
alert("The shift key was NOT pressed!")
}
}
function whichElement(e)
{
var targ;
if(!e) var e=window.event;
if(e.target)
{
targ=e.target
}else if(e.srcElement)
{
targ=e.srcElenment
}else if(targ.nodeType==3)
{
targ=targ.parentNode;
}
var tname;
tname=targ.tagName;
alert("您点击了"+tname+"元素");
}
//event.type 判断事件类型
</script>
</head>
//获取键盘的unicode<body onkeyup="whichbutton(event)">
//判断鼠标点击的键<body onmousedown="which_button(event)">
//显示鼠标点击的坐标<body onmousedown="show_coords(event)">
//显示鼠标相对于屏幕的坐标
<body onmousedown="coordinates(event)">
//判断是否按了shift键
<body onmousedown="isKeyPressed(event)">
//判断光标点了哪些元素
<body onmousedown="whichElement(event)">
<p>sadfsd</p>
<hr>
<br>
<h1>sadfsdf</h1>
<?php ?>
</body>
</html>
2,钟表小例子
代码如下:
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementByIdx_x_x_x('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body onLoad="startTime()">
<div id="txt">脚本学堂,欢迎大家的光临。</div>
</body>
</html>