如何在 jQuery 小部件上设置选项
我正在尝试为 Wijmo 中几乎未记录的复选框小部件设置选项。这是小部件的代码:
(function ($) {
"use strict";
var checkboxId = 0;
$.widget("wijmo.wijcheckbox", {
_csspre: "wijmo-checkbox",
_init: function () {
var self = this,
ele = self.element,
o = self.options,
checkboxElement, label, targetLabel, boxElement, iconElement;
if (ele.is(":checkbox")) {
if (!ele.attr("id")) {
ele.attr("id", self._csspre + checkboxId);
checkboxId += 1;
}
if (ele.parent().is("label")) {
checkboxElement = ele.parent()
.wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
.parent()
.wrap("<div></div>").parent().addClass(self._csspre + " ui-widget");
label = ele.parent();
label.attr("for", ele.attr("id"));
checkboxElement.find("." + self._csspre + "-inputwrapper")
.append(ele);
checkboxElement.append(label);
}
else {
checkboxElement = ele
.wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
.parent().wrap("<div></div>").parent()
.addClass(self._csspre + " ui-widget");
}
targetLabel = $("label[for='" + ele.attr("id") + "']");
// if (targetLabel.length > 0) {
// checkboxElement.append(targetLabel);
// targetLabel.attr("labelsign", "C1");
// }
if (ele.is(":disabled")) {
self._setOption("disabled", true);
}
boxElement = $("<div class='" + self._csspre +
"-box ui-widget ui-state-" +
(o.disabled ? "disabled" : "default") +
" ui-corner-all'><span class='" +
self._csspre + "-icon'></span></div>");
iconElement = boxElement.children("." + self._csspre + "-icon");
checkboxElement.append(boxElement);
ele.data("iconElement", iconElement);
ele.data("boxElement", boxElement);
boxElement.removeClass(self._csspre + "-relative")
.attr("role", "checkbox")
.bind("mouseover", function () {
ele.mouseover();
}).bind("mouseout", function () {
ele.mouseout();
});
if (targetLabel.length === 0 || targetLabel.html() === "") {
boxElement.addClass(self._csspre + "-relative");
}
ele.bind("click.checkbox", function (e) {
self.refresh(e);
}).bind("focus.checkbox", function () {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-default").addClass("ui-state-focus");
}).bind("blur.checkbox", function () {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-focus").not(".ui-state-hover")
.addClass("ui-state-default");
}).bind("keydown.checkbox", function (e) {
if (e.keyCode === 32) {
if (o.disabled) {
return;
}
self.refresh();
}
});
boxElement.bind("click.checkbox", function (e) {
ele.get(0).checked = !ele.get(0).checked;
ele.change();
ele.focus();
self.refresh(e);
});
self.refresh();
checkboxElement.bind("mouseover.checkbox", function (e) {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-default").addClass("ui-state-hover");
}).bind("mouseout.checkbox", function (e) {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-hover").not(".ui-state-focus")
.addClass("ui-state-default");
});
}
},
refresh: function (e) {
var self = this;
self.element.data("iconElement")
.toggleClass("ui-icon ui-icon-check", self.element.get(0).checked);
self.element.data("boxElement")
.toggleClass("ui-state-active", self.element.get(0).checked)
.attr("aria-checked", self.element.get(0).checked);
if (e) {
e.stopPropagation();
}
},
destroy: function () {
var self = this, boxelement = self.element.parent().parent();
boxelement.children("div." + self._csspre + "-box").remove();
self.element.unwrap();
self.element.unwrap();
$.Widget.prototype.destroy.apply(self);
}
});
} (jQuery));
有人可以帮助我告诉我如何在小部件工厂创建的小部件上设置选项吗?我找不到任何可以给我建议的东西。
更新:
我发布了完整的代码。评论区都是我的评论。
I am trying to set the options for the almost UNDOCUMENTED checkbox widget from Wijmo. Here's the code for the widget:
(function ($) {
"use strict";
var checkboxId = 0;
$.widget("wijmo.wijcheckbox", {
_csspre: "wijmo-checkbox",
_init: function () {
var self = this,
ele = self.element,
o = self.options,
checkboxElement, label, targetLabel, boxElement, iconElement;
if (ele.is(":checkbox")) {
if (!ele.attr("id")) {
ele.attr("id", self._csspre + checkboxId);
checkboxId += 1;
}
if (ele.parent().is("label")) {
checkboxElement = ele.parent()
.wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
.parent()
.wrap("<div></div>").parent().addClass(self._csspre + " ui-widget");
label = ele.parent();
label.attr("for", ele.attr("id"));
checkboxElement.find("." + self._csspre + "-inputwrapper")
.append(ele);
checkboxElement.append(label);
}
else {
checkboxElement = ele
.wrap("<div class='" + self._csspre + "-inputwrapper'></div>")
.parent().wrap("<div></div>").parent()
.addClass(self._csspre + " ui-widget");
}
targetLabel = $("label[for='" + ele.attr("id") + "']");
// if (targetLabel.length > 0) {
// checkboxElement.append(targetLabel);
// targetLabel.attr("labelsign", "C1");
// }
if (ele.is(":disabled")) {
self._setOption("disabled", true);
}
boxElement = $("<div class='" + self._csspre +
"-box ui-widget ui-state-" +
(o.disabled ? "disabled" : "default") +
" ui-corner-all'><span class='" +
self._csspre + "-icon'></span></div>");
iconElement = boxElement.children("." + self._csspre + "-icon");
checkboxElement.append(boxElement);
ele.data("iconElement", iconElement);
ele.data("boxElement", boxElement);
boxElement.removeClass(self._csspre + "-relative")
.attr("role", "checkbox")
.bind("mouseover", function () {
ele.mouseover();
}).bind("mouseout", function () {
ele.mouseout();
});
if (targetLabel.length === 0 || targetLabel.html() === "") {
boxElement.addClass(self._csspre + "-relative");
}
ele.bind("click.checkbox", function (e) {
self.refresh(e);
}).bind("focus.checkbox", function () {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-default").addClass("ui-state-focus");
}).bind("blur.checkbox", function () {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-focus").not(".ui-state-hover")
.addClass("ui-state-default");
}).bind("keydown.checkbox", function (e) {
if (e.keyCode === 32) {
if (o.disabled) {
return;
}
self.refresh();
}
});
boxElement.bind("click.checkbox", function (e) {
ele.get(0).checked = !ele.get(0).checked;
ele.change();
ele.focus();
self.refresh(e);
});
self.refresh();
checkboxElement.bind("mouseover.checkbox", function (e) {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-default").addClass("ui-state-hover");
}).bind("mouseout.checkbox", function (e) {
if (o.disabled) {
return;
}
boxElement.removeClass("ui-state-hover").not(".ui-state-focus")
.addClass("ui-state-default");
});
}
},
refresh: function (e) {
var self = this;
self.element.data("iconElement")
.toggleClass("ui-icon ui-icon-check", self.element.get(0).checked);
self.element.data("boxElement")
.toggleClass("ui-state-active", self.element.get(0).checked)
.attr("aria-checked", self.element.get(0).checked);
if (e) {
e.stopPropagation();
}
},
destroy: function () {
var self = this, boxelement = self.element.parent().parent();
boxelement.children("div." + self._csspre + "-box").remove();
self.element.unwrap();
self.element.unwrap();
$.Widget.prototype.destroy.apply(self);
}
});
} (jQuery));
Can someone help me by telling me how to set options on a widget created by the widget factory. I can find nothing out there that gives me advice.
Update:
I posted the complete code. The commented area are my comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看插件(小部件)的源代码,您似乎只有一个可配置的选项参数。它被称为
disabled
并且您可以这样使用它...文档中可能没有提及,因为如果元素的
,插件会自动将其设置为“true”禁用
属性已在其他地方设置。根据OP对原始帖子的评论进行编辑:
逗号后面的项目列表不是
o< 的任何部分/code>、
self.options
或任何用户可配置的选项或参数。它们只是插件内部的附加变量声明。换句话说,这...
只是简写。更高效的代码)为此......
o
是表示插件的选项或参数的变量。Looking at the source code of the plugin (widget), it appears that you only have one configurable option parameter. It's called
disabled
and you could use it like this...It's probably not mentioned in the documentation because the plugin sets it to "true" automatically if the element's
disabled
property is already set elsewhere.EDIT based on OP's comments on original posting:
The list of items after the comma are not any part of
o
,self.options
, or any user configurable options or parameters. They are just additional variable declarations internal to the plugin.In other words, this...
is simply shorthand (and more efficient code) for this...
o
is the variable that represents the options or parameters for the plugin.