7

我正在使用ColorPicker 插件。我用以下代码初始化了插件:

$(".colorpic").ColorPicker({
    color: '#0000ff',
    onShow: function (colpkr) {
        $(colpkr).fadeIn(500);
        return false;
    },
    onHide: function (colpkr) {
        $(colpkr).fadeOut(500);
        return false;
    },
    onChange: function (hsb, hex, rgb) {
        $(this).css('backgroundColor', '#' + hex);  <= $(this) not working 
    }
});

现在我的问题是这在事件$(this)中不起作用。onchange请帮帮我好吗?

4

2 回答 2

9

试试这样:

$(".colorpic").each(function(){
    var $this = $(this);

    $this.ColorPicker({
        color: '#0000ff',
        onShow: function (colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function (colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function (hsb, hex, rgb) {
            $this.css('backgroundColor', '#' + hex);
        }
    });
});
于 2012-06-19T13:18:02.663 回答
3

The this is a really big problem, since in this case the this goes to the function if I am not mistaken. Try the following:

var colorPicker=this;
onChange: function (hsb, hex, rgb) {
    $(colorPicker).css('backgroundColor', '#' + hex); 
}
于 2012-06-19T13:17:05.737 回答