<!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=gb2312" />
<title>js注册机</title>
<script language="javascript" type="text/javascript">
//链表对象-----------------------------------------------------------------------------------------------------------------------
function Entry(next, data)
{
this.next = next
this.data = data
}
function Iterator(node)
{
this.cousor = node
this.hasNext = function ()
{
return (this.cousor.next != null);
}
this.next = function ()
{
var rt = this.cousor.next
this.cousor = this.cousor.next
return rt.data
}
this.replaceData=function(datar){
this.cousor.data=datar
}
}
function LinkedList()
{
this.head = new Entry(null, null)
this.size = function ()
{
var size = 0
if (this.head == null)
{
return size
}
var p = this.head.next
for(; p!=null; p = p.next)
size++;
return size;
}
this.clear = function ()
{
this.head = null
}
this.getNode = function (idx)
{
var pos = -1;
var p = this.head
while (p != null && pos < idx) {
p = p.next;
pos ++;
}
return p;
}
this.gett = function (idx)
{
return this.getNode(idx).data
}
this.add = function (data)
{
this.insert(this.size(), data)
}
this.insert = function (idx, data)
{
var p = this.getNode(idx-1); /*注意查询idx-1*/
if (p == null){
return
}
var node = new Entry(p.next, data)
p.next = node
}
this.remove = function (idx)
{
var prenode = this.getNode(idx - 1)
var node = this.getNode(idx)
if (prenode == null || node == null)
{
return null
}
prenode.next = node.next
return node.data
}
this.iterator = function ()
{
return new Iterator(this.head)
}
this.swap = function (a, b)
{
var av = this.getNode(a)
var bv = this.getNode(b)
var tmp = av.data
av.data = bv.data
bv.data = tmp
}
this.replace=function(index,data){
var j=0;
for(var itr =this.iterator(); itr.hasNext(); ){
itr.next();
if(index==j) itr.replaceData(data);
j++;
}
}
}
//链表对象-----------------------------------------------------------------------------------------------------------------------
</script>
</head>
<input type="button" onclick="registerMove()" value="注册移动事件" />
<input type="button" onclick="registerStop()" value="注册停止事件" />
<input type="button" onclick="registerClose()" value="注册关闭事件" />
<input type="button" onclick="registerAll()" value="注册所有事件" />
<br/>
<br/>
请输入删除事件的ID:<input type="textbox" id="eventDelID" /> <input type="button" onclick="registerDel()" value="删除事件" />
<br/>
<br/>
<input type="button" onclick="doEvent()" value="触发器" />
<body>
<script language="javascript" type="text/javascript">
var oneregister=new register();
function register(){
this.event=""; //事件执行代码
this.eventList=new LinkedList();//用于储存事件的链表 属于注册机的属性
this.addEvent=function(evnet){//将事件名加到事件链表中
for(var itr = this.eventList.iterator();itr.hasNext(); )//如果id有重复的事件者不注册
{
var itm = itr.next();
if(itm.id==evnet.id){
return "";
}
}
this.eventList.add(evnet);
}
this.delEvent=function(id){//将事件名加到事件链表中
var i=0;
for(var itr = this.eventList.iterator();itr.hasNext(); )//如果id有重复的事件者不注册
{
var itm = itr.next();
if(itm.id==id){
this.eventList.remove(i);
return "";
}
i++;
}
}
this.doEvent=function(){
//循环执行注册机内的事件
for(var itr = this.eventList.iterator();itr.hasNext(); )
{
var itm = itr.next();
eval(itm.value);
}
}
}
//事件对象
function event(){
this.id="";
this.value="alert(\"默认事件!\")";
}
function registerMove(){
var e=new event();
e.id="move";
e.value="alert(\"执行了移动事件!\")";
oneregister.addEvent(e);
}
function registerStop(){
var e=new event();
e.id="stop";
e.value="alert(\"执行了停止事件!\")";
oneregister.addEvent(e);
}
function registerClose(){
var e=new event();
e.id="close";
e.value="eventClose()";
oneregister.addEvent(e);
}
function registerAll(){
var e=new event();
e.id="move";
e.value="alert(\"执行了移动事件!\")";
oneregister.addEvent(e);
var e=new event();
e.id="stop";
e.value="alert(\"执行了停止事件!\")";
oneregister.addEvent(e);
var e=new event();
e.id="close";
e.value="eventClose()";
oneregister.addEvent(e);
}
function eventClose(){
alert("执行了关闭事件");
}
//删除事件
function registerDel(){
var id=document.getElementById('eventDelID').value;
oneregister.delEvent(id);
}
//触发器
function doEvent(){
oneregister.doEvent();
}
</script>
</body>
</html>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛