$(这个)不起作用

时间:2012-06-19 13:15:33

标签: javascript jquery

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

$(".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事件中无效。请帮帮我?

2 个答案:

答案 0 :(得分: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);
        }
    });
});

答案 1 :(得分:3)

this是一个非常大的问题,因为在这种情况下,如果我没有弄错的话,this会转到该函数。 请尝试以下方法:

var colorPicker=this;
onChange: function (hsb, hex, rgb) {
    $(colorPicker).css('backgroundColor', '#' + hex); 
}