我在我的项目中使用以下jquery颜色选择器-www.eyecon.ro/colorpicker。我在同一页面上使用多个拾色器,在一个类上调用脚本(我有7个输入应用了拾色器,而它们都不相关)。我遇到的问题是我无法访问触发器,因为选色器与其触发器之间没有关系。
有谁知道如何做到这一点?基本上我正在尝试使用此功能
(...)onChange: function (hsb, hex, rgb) {
$(this).css('backgroundColor', '#' + hex);
}
$(this)应该是父项,但显然不是。
最佳答案
您需要将函数绑定到上下文,
所以在调用函数之前,先声明
var that = this // or whatever this(the context) is
然后在回调函数中而不是在此方法中调用它。
...)onChange: function (hsb, hex, rgb) {
$(that).css('backgroundColor', '#' + hex);
}
编辑:这是我会写的javascript代码。
$(document).ready(function() {
$('.colorpickerHolder').each(function(o) {
var _this = this;
$(this).ColorPicker({
onChange: function(hsb, hex, rgb) {
_this.style.background = '#' + hex;
// the input which is trigger the colorpicker is supposed to be $(THIS);
}
})
})
});
http://jsfiddle.net/camus/PQDf8/4/