将参数传递给绑定到事件的 javascript 函数
Pass parameters to javascript functions which is bind to events
我有这样的功能:
var that = this;
this.context.on('focus', function(i) {
var htmlElement = ele;
var style = {left: 'auto', color: '#000000'};
if(i !== null) {
i = that.context.size()/2;
style.left = i + 'px';
//find htmlElement in DOM and apply style to it
}
});
htmlElement 和样式在不同的调用中会因我而异。我需要传递一个选项,例如
var options = {style: someVaue, htmlELement: somelement}
添加到函数中,以便可以使用不同的选项多次调用它。如果我像这样通过 options
:
var that = this;
this.context.on('focus', function(i, options) {
//use options.style and options.htmlElement
});
这样不行。显然我做错了什么,因为 function
绑定到 this.context
的 focus
事件具有 i
作为参数但不能接受任何其他参数。任何形式的 help/suggestion 都表示赞赏。
我想我已经找到了解决办法。感谢大家的帮助。以下是如何传递选项:
var that = this;
this.context.on('focus', function(i) {
var options = {i: i, style: someValue, htmlElement: someElement};
that.setStyle(options);
});
setStyle: function(options) {
var htmlElement = options.htmlElement;
var style = options.style;
if(options.i !== null) {
options.i = that.context.size()/2;
style.left = i + 'px';
//find htmlElement in DOM and apply style to it
}
}
欢迎大家多多评论!!!
我有这样的功能:
var that = this;
this.context.on('focus', function(i) {
var htmlElement = ele;
var style = {left: 'auto', color: '#000000'};
if(i !== null) {
i = that.context.size()/2;
style.left = i + 'px';
//find htmlElement in DOM and apply style to it
}
});
htmlElement 和样式在不同的调用中会因我而异。我需要传递一个选项,例如
var options = {style: someVaue, htmlELement: somelement}
添加到函数中,以便可以使用不同的选项多次调用它。如果我像这样通过 options
:
var that = this;
this.context.on('focus', function(i, options) {
//use options.style and options.htmlElement
});
这样不行。显然我做错了什么,因为 function
绑定到 this.context
的 focus
事件具有 i
作为参数但不能接受任何其他参数。任何形式的 help/suggestion 都表示赞赏。
我想我已经找到了解决办法。感谢大家的帮助。以下是如何传递选项:
var that = this;
this.context.on('focus', function(i) {
var options = {i: i, style: someValue, htmlElement: someElement};
that.setStyle(options);
});
setStyle: function(options) {
var htmlElement = options.htmlElement;
var style = options.style;
if(options.i !== null) {
options.i = that.context.size()/2;
style.left = i + 'px';
//find htmlElement in DOM and apply style to it
}
}
欢迎大家多多评论!!!