jQuery的范围是each()函数吗? [英] Scope of jQuery each() function?
问题描述
我正在使用jQuery ColorPicker小部件 - 专门运用ColorPickerSetColor函数(内部只是'setColor')。代码摘录:
I'm working with the jQuery ColorPicker widget - specifically exercising the ColorPickerSetColor function (just 'setColor' internally). Code excerpt:
setColor: function(col) {
if (typeof col == 'string') {
col = HexToHSB(col);
} else if (col.r != undefined && col.g != undefined && col.b != undefined) {
col = RGBToHSB(col);
} else if (col.h != undefined && col.s != undefined && col.b != undefined) {
col = fixHSB(col);
} else {
return this;
}
return this.each(function(){
if ($(this).data('colorpickerId')) {
var cal = $('#' + $(this).data('colorpickerId'));
cal.data('colorpicker').color = col;
cal.data('colorpicker').origColor = col;
fillRGBFields(col, cal.get(0));
fillHSBFields(col, cal.get(0));
fillHexFields(col, cal.get(0));
setHue(col, cal.get(0));
setSelector(col, cal.get(0));
setCurrentColor(col, cal.get(0));
setNewColor(col, cal.get(0));
}
});
}
似乎小部件中存在错误。在每个()调用内部检查时,'col'参数未定义。我已经阅读了文档和其他示例,我可以找到的所有内容表明,当每个()调用执行函数时,'col'仍应在范围内,但它似乎不是......
It seems that there is a bug in the widget. The 'col' parameter, when inspected inside of the each() call, is undefined. I've read the documentation and other examples, and everything I can find indicates that 'col' should still be in scope when the each() call executes the function, but it doesn't seem to be...
帮助?
谢谢!
推荐答案
我最后在一个已知良好的范围(setColor函数的父级)中使用了一个临时变量。绝对是一个黑客,但它的工作原理。如果有人知道如何以正确的方式解决这个问题,请告诉我。 :)
I just ended up using a temporary variable in a known-good scope (the parent of the setColor function). Definitely a hack, but it works. If anyone knows how to fix this the right way, please let me know. :)
谢谢!
-Rich
Thanks! -Rich
这篇关于jQuery的范围是each()函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!