2012-06-19 40 views
7

estoy usando ColorPicker Plugin. que inicializa el plugin con el siguiente código:

$(".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 
    } 
}); 

Ahora mi problema es que no está funcionando $(this) en onchange evento. Ayudame por favor?

+0

quizás usted necesita '' unirse a this' onChange'? – tkone

+1

Es posible que el complemento no pase su contexto al método 'onChange'. Abra un problema :) – Terry

Respuesta

9

Trate de esta manera:

$(".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); 
     } 
    }); 
}); 
+0

gracias por la respuesta –

3

El this es un problema realmente grande, ya que en este caso el this va a la función si no me equivoco. intente lo siguiente:

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