2012-06-19 13 views
7

ich ColorPicker Plugin. verwende ich das Plugin mit folgendem Code initialisiert:

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

Nun mein Problem ist, dass $(this) nicht in onchange Veranstaltung arbeiten. Hilf mir bitte?

+0

vielleicht müssen Sie 'zu this' zu' onChange' binden? – tkone

+1

Es ist möglich, dass das Plugin seinen Kontext nicht an die 'onChange' Methode übergibt. Öffnen Sie ein Problem :) – Terry

Antwort

9

wie diese versuchen:

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

danke für die Antwort –

3

Die this ist ein wirklich großes Problem, da in diesem Fall die this an die Funktion geht, wenn ich mich nicht irre. Versuchen Sie Folgendes:

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