⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custombutton.js

📁 ajax实例,可以了解ajax的原理
💻 JS
字号:
Type.registerNamespace("Demo");

Demo.CustomButton = function(element) 
{
    // 该类继承自Sys.UI.Control,调用基类构造函数
    Demo.CustomButton.initializeBase(this, [element]);

    this._clickDelegate = null;
    this._hoverDelegate = null;
    this._unhoverDelegate = null;
}
Demo.CustomButton.prototype = 
{
    // 定义text属性 - 元素显示的信息
    get_text: function() 
    {
        // element - Sys.UI.Control的属性
        return this.get_element().innerHTML;
    },
    set_text: function(value) 
    {
        this.get_element().innerHTML = value;
    },

    // 添加或移除click事件
    add_click: function(handler) 
    {
        // events - Sys.Component的属性
        this.get_events().addHandler('click', handler);
    },
    remove_click: function(handler) 
    {
        this.get_events().removeHandler('click', handler);
    },
    
    // 添加或移除hover事件
    add_hover: function(handler) 
    {
        this.get_events().addHandler('hover', handler);
    },
    remove_hover: function(handler) 
    {
        this.get_events().removeHandler('hover', handler);
    },

    // 添加或移除unhover事件
    add_unhover: function(handler) 
    {
        this.get_events().addHandler('unhover', handler);
    },
    remove_unhover: function(handler) 
    {
        this.get_events().removeHandler('unhover', handler);
    },

    // 释放资源
    dispose: function() 
    {
        var element = this.get_element();

        if (this._clickDelegate) 
        {
            // Sys.UI.DomEvent removeHandler()
            $removeHandler(element, 'click', this._clickDelegate);
            delete this._clickDelegate;
        }

        if (this._hoverDelegate) 
        {
            $removeHandler(element, 'focus', this._hoverDelegate);
            $removeHandler(element, 'mouseover', this._hoverDelegate);
            delete this._hoverDelegate;
        }

        if (this._unhoverDelegate) 
        {
            $removeHandler(element, 'blur', this._unhoverDelegate);
            $removeHandler(element, 'mouseout', this._unhoverDelegate);
            delete this._unhoverDelegate;
        }
        Demo.CustomButton.callBaseMethod(this, 'dispose');
    },

    // 初始化
    initialize: function() 
    {
        var element = this.get_element();

        if (!element.tabIndex) element.tabIndex = 0;

        if (this._clickDelegate === null) 
        {
            // Function.createDelegate用来创建一个调用“this”上下文下的特定函数的委托
            this._clickDelegate = Function.createDelegate(this, this._clickHandler);
        }
        // Sys.UI.DomEvent addHandler()
        $addHandler(element, 'click', this._clickDelegate);

        if (this._hoverDelegate === null) 
        {
            this._hoverDelegate = Function.createDelegate(this, this._hoverHandler);
        }
        $addHandler(element, 'mouseover', this._hoverDelegate);
        $addHandler(element, 'focus', this._hoverDelegate);

        if (this._unhoverDelegate === null) 
        {
            this._unhoverDelegate = Function.createDelegate(this, this._unhoverHandler);
        }
        $addHandler(element, 'mouseout', this._unhoverDelegate);
        $addHandler(element, 'blur', this._unhoverDelegate);

        Demo.CustomButton.callBaseMethod(this, 'initialize');
    },
    
    // click事件处理器
    _clickHandler: function(event) 
    {
        var h = this.get_events().getHandler('click');
        if (h) h(this, Sys.EventArgs.Empty);
    },
    // hover事件处理器
    _hoverHandler: function(event) 
    {
        var h = this.get_events().getHandler('hover');
        if (h) h(this, Sys.EventArgs.Empty);
    },
    // unhover事件处理器
    _unhoverHandler: function(event) 
    {
        var h = this.get_events().getHandler('unhover');
        if (h) h(this, Sys.EventArgs.Empty);
    }
}
Demo.CustomButton.registerClass('Demo.CustomButton', Sys.UI.Control);

// 通知ScriptManager这段脚本已经加载完毕
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -