在开发Ajax应用程序中,如果可以自定义Javascript事件,那将是一件非常愉快的事情,不过要将JS开发成MFC或者C#那样的事件处理程序确实非常的困难。于是Google一下,找到了一些Javascript自定义事件开发的方法,写了一个简单的事例程序:
    
  
<html>
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        
<script src="yahoo-min.js"></script>
        
<script src="event-min.js"></script>
        
<script language="JavaScript">
            
var onLogin=new YAHOO.util.CustomEvent("onlogin");
            
function hiLogin()
            {
                alert(
"hello");
            }
            
function hiLogin1()
            {
                alert(
"Hello111");
            }
            onLogin.subscribe(hiLogin);    
            onLogin.subscribe(hiLogin);
            onLogin.subscribe(hiLogin1);
            
            
function btcl()
            {
                window.status
="click button";
                onLogin.fire();
            }    
        
</script>
    
</head>
    
<body>
        
<input type="button" title="Click me" onclick="btcl()" value="Click me"/>
    
</body>
</html>

说明:
      1 使用了Yahoo的JS开发包,Yahoo的js文件一般都有两个,一个是原版,一个是min版本,min版本和原版的功能是完全一样,至于为什么这么小呢,因为删除了所有的注释、换行、空格,并且变量的命名也用最少的字符表示,所以小了很多。
      2 Yahoo的事件包包含了普通的事件订阅等功能,使用时比自己开发一套事件路由程序要方便的多。

便于分析,帖了两个JS包的类结构图如下
Yahoo.js:

在Javascriptz中自定义事件
Event.js:

在Javascriptz中自定义事件
参考地址:
http://developer.yahoo.com/yui/event/ Yahoo
http://www.dustindiaz.com/custom-events/  Web Standards with Imagination

源代码下载
Yahoo_Event_Test

相关文章:

  • 2021-09-24
  • 2022-01-27
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-02-03
  • 2022-01-04
相关资源
相似解决方案