日期:2014-05-18  浏览次数:20384 次

如何给函数增加事件??
function createLayer(name, inleft)
 {
。。。。。。。
}
如果有一函数我现在想给它增加onmouseover及onmouseout事件,怎么做呢?在哪里写语句?

------解决方案--------------------

这是鼠标事件,当鼠标接触按钮的时候就会触发这个事件。
你只要在你需要触发事件的按钮上加上这事件就行了

例如:
<a href='#' onmouseover=createLayer('','')></a>
------解决方案--------------------
自己参考一下:

JScript code

//----------------------------------------------------------------
    //    定义类Class
    
    function Class()
    {
        this.name = "Class";
        this.__handlers__ = {};
    }
    
    
    //定义事件
    Class.prototype.show = function(name)
    {
        
    }
    
    //事件处理函数
    Class.prototype.onShow = function(name)
    {
        if (typeof this.__handlers__["onShow"] != "undefined")
        {
            for (var i = 0; i < this.__handlers__["onShow"].length; i++)
            {
                this.__handlers__["onShow"][i](name);
            }
        }
    }
    
    //添加事件处理函数
    Class.prototype.addEventHandler = function(eventName, handler)
    {
        if (typeof this.__handlers__[eventName] == "undefined")
        {
            this.__handlers__[eventName] = new Array();
        }
        this.__handlers__[eventName].push(handler);
    }
    
    //    类Class定义结束
    //----------------------------------------------------------------
    
//------测试

    //处理函数一
    function handler1(name)
    {
        alert("1: " + name);
    }
    
    //处理函数二
    function handler2(name)
    {
        alert("2: " + name);
    }

    //实例一个对象c
    var c = new Class(); 
    
        //给onShow事件添加事件处理函数
    c.addEventHandler("onShow", handler1);
    c.addEventHandler("onShow", handler2);
    
    //触发事件
    c.onShow("hello");

------解决方案--------------------
那我写onmouseover、onmouseout内容的语句,放在什么地方?
========================================================

去看一下dhtml手册,放在任何支持这两个事件的html元素里面。