简单的 Mootools 到 jQuery 转换 - 事件处理程序
我正在将 MooTools 插件移植到 jQuery,但在我陷入一小部分之前从未使用过 MooTools,我希望有人可以提供帮助。
MooTools:
var bt = new Element('a',{
"title" : label,
"text" : label,
"class" : classe,
"events": {
click: (clickEvent || this.hide).bind(this)
}
});
this.buttons.push(btn);
return btn;
到目前为止我的 jQuery:
var btn = $('a').attr({
"title" : label,
"text" : label,
"class" : cssClass
});
btn.click(function() {
});
我只是不确定如何从 MooTools 转换点击处理程序行...?
编辑:这是完整的功能:
addButton : function(label, cssClass, clickEvent) {
// jQuery code
var btn = $('<a></a>').attr({
"title" : label,
"text" : label,
"class" : cssClass
});
btn.click(function() {
});
// Mootools code
var bt = new Element('a',{
"title" : label,
"text" : label,
"class" : classe,
"events": {
click: (clickEvent || this.hide).bind(this)
}
});
this.buttons.push(btn);
return btn;
},
它是模态窗口插件的一部分,因此添加这一点它会检查要添加的按钮。如果回调函数被传递,那么它将对其起作用,如果没有,那么它将隐藏模态窗口...我认为...
注意:只是指出我还稍微改变了jQuery 版本中的参数和变量名称。这不是一个错字。
I'm in the middle of porting a MooTools plugin over to jQuery but having never used MooTools before I'm stuck on a small section which I'm hoping someone can help with.
MooTools:
var bt = new Element('a',{
"title" : label,
"text" : label,
"class" : classe,
"events": {
click: (clickEvent || this.hide).bind(this)
}
});
this.buttons.push(btn);
return btn;
My jQuery so far:
var btn = $('a').attr({
"title" : label,
"text" : label,
"class" : cssClass
});
btn.click(function() {
});
I'm just not sure how to convert the click handler line from MooTools...?
EDIT: Here's the full function:
addButton : function(label, cssClass, clickEvent) {
// jQuery code
var btn = $('<a></a>').attr({
"title" : label,
"text" : label,
"class" : cssClass
});
btn.click(function() {
});
// Mootools code
var bt = new Element('a',{
"title" : label,
"text" : label,
"class" : classe,
"events": {
click: (clickEvent || this.hide).bind(this)
}
});
this.buttons.push(btn);
return btn;
},
It's part of a Modal window plugin so add this point it's checking to see what buttons it's meant to be adding. If the callback function is passed then it will act on it, if not then it will hide the Modal window... I think...
NOTE: Just to point out I've also slightly changed the parameter and variable names in the jQuery version. It's not a typo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
或者,你可以这样写:
或者,像这样:
Try this:
Or, you could write it like this:
Or, like this:
这部分需要重写。 mootools插件实际上是在创建一个新的锚点;您拥有的代码将选择页面上的所有锚点并将属性应用于它们(实际上它可能只会影响第一个选定的锚点,但仍然是一个问题)。
它不能回答你的问题,但这是你需要知道的事情。
This part needs to be rewritten. The mootools plugin is actually creating a new anchor; the code you have is going to select all anchors on the page and apply the attrs to them (actually it probably just affects the first selected anchor, but still a problem).
It doesn't answer your question, but it's something you needed to know.
注意:
$.proxy( fn, context )
在功能上等同于fn.bind( context )
使用
.click(fn)
更好> 比attr("click", fn)
因为前者得到jQuery 跨浏览器事件修复。
Notes:
$.proxy( fn, context )
is functionally equivalent tofn.bind( context )
It's much better to use
.click(fn)
thanattr("click", fn)
because the former getsjQuery cross browser event fixes.