javascript - 需要帮助创建 jquery 插件

标签 javascript jquery jquery-plugins

我正在创建一个名为“togglebutton”的简单 jquery 插件。我的问题在底部。 这是代码。

/** CSS */
.toggle-button{
    -moz-border-radius:4px;
    border-radius:4px;
    border:solid gray 1px;
    padding:4px;
    color:#fff; 
}
.state-on{ background:gray; }

.state-off{ background:blue; }


/** JQuery Plugin Definition */
jQuery.fn.togglebutton = function (options) {
    myoptions = jQuery.extend ({
    state: "off",
    }, options);

    this.click(function(){
        myoptions.click;
        opt = options;
        $this = $(this);
        if(opt.state == 'on'){
            turnoff($this);
            opt.state = 'off';
        }
        else if(opt.state == 'off'){
            turnon($this);
            opt.state = 'on';
        }
    });

    this.each(function () {
        this.options = myoptions;
        $this = $(this);
        opt = this.options;

        if(opt.state == 'on'){
            $this.attr('class', "toggle-button state-on");
        }
        else if(opt.state == 'off'){
            $this.attr('class', "toggle-button state-off");
        }

    });

    function turnon($this){
        $this.attr('class', "toggle-button state-on");
    }
    function turnoff($this){
        $this.attr('class', "toggle-button state-off");
    }
}

/** How to Call */
$('#crop').togglebutton({state:'off'});

我将在 jquery 插件定义中添加什么,以便我可以将其命名为:

$('#crop').togglebutton({
    state:'off',
    click: function(event, ui) {
        val = ui.value;
    }
});

非常感谢任何帮助。我是这类东西的新手。

最佳答案

开始方式示例:

// plugin definition
$.fn.togglebutton = function() {
  // Your plugin implementation code goes here.
};

这是一个很棒的教程,而且很容易实现

http://www.learningjquery.com/2007/10/a-plugin-development-pattern

关于javascript - 需要帮助创建 jquery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10446135/

相关文章:

javascript - 如何在单个页面应用程序上有多个 css 打印布局?

javascript - 如何通过 javascript 或 jquery 隐藏源代码和检查元素?

javascript - 如何像这样创建自定义 jQuery

javascript - 不带元素的jQuery插件方法

javascript - Jquery提交表单损坏

javascript - 加载图像间歇性失败 - ERR_CONTENT_LENGTH_MISMATCH

php - 文件上传请求中的 Content-Transfer-Encoding

javascript - 一些 TD 移动到新行后的多个 TD

javascript - 将单个参数传递给具有默认值的函数

jquery-bootgrid 如何隐藏列