【发布时间】:2015-05-12 09:56:39
【问题描述】:
我有这样的功能:
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
});
这样不行。显然我做错了,因为this.context 的function 绑定到focus 事件将i 作为参数但不能接受任何其他参数。任何形式的帮助/建议表示赞赏。
【问题讨论】:
-
在这种情况下,我通常将
data-*属性添加到元素,然后在所需的事件处理程序下检索它们。 -
@RoryMcCrossan 仅适用于 HTML5 代码因为 W3C 验证是很好的解决方案。
-
@RoryMcCrossan :检索元素不是我的问题。将其传递给可以接受参数的函数就是一个。
-
@KrzysztofTrzos 没错,尽管现在 HTML5 已得到广泛支持。
-
是你要找的东西吗:
this.context.on('focus', function(options, e) { //use options.style and options.htmlElement }.bind(this, options));?
标签: javascript jquery cubism.js