gpt4 book ai didi

javascript - 在创建 jquery 插件时需要帮助

转载 作者:行者123 更新时间:2023-11-29 22:24:28 26 4
gpt4 key购买 nike

我正在创建一个名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com