2013-08-24 129 views
0

我使用jPicker插件從拾取器中獲取顏色。 我以這種方式創建元素:jPicker設置顏色

  $(function(){ 
       $('#txtBackgroundColor').jPicker(
       { 
        color: 
        { 
         mode: 'h', // acceptable values "h" (hue), "s" (saturation), "v" (brightness), "r" (red), "g" (green), "b" (blue), "a" (alpha) 
         active: new $.jPicker.Color({ hex: 'eaeaea' }), // accepts any declared jPicker.Color object or hex string WITH OR WITHOUT '#' 
        }, 
        window: 
        { 
         position: 
         { 
          x: 'screenCenter', // acceptable values "left", "center", "right", "screenCenter", or relative px value 
          y: '200px', // acceptable values "top", "bottom", "center", or relative px value 
         }, 
         expandable: true 
        }, 
       }, 

      }); 

當我點擊一個按鈕,我想設置jpicker的活躍色彩。我已經在文件中看到這一行:

$('#update').click(function(){ 
    $.jPicker.List[0].color.active.val('hex', 'e2ddcf', this); 
}); 

但問題是,我有多個jPicker,我不知道該列表例如指數,是有辦法通過設置jPicker顏色編號而不是索引列表?

謝謝

回答

-1

你的語法不對。修復。

你可以使用。每()指定這類每一個元素:

$(function() { 
$('#txtBackgroundColor').each(function() { 
    $(this).jPicker({ 
     color: { 
      mode: 'h', // acceptable values "h" (hue), "s" (saturation), "v" (brightness), "r" (red), "g" (green), "b" (blue), "a" (alpha) 
      active: new $.jPicker.Color({ 
       hex: 'eaeaea' 
      }), // accepts any declared jPicker.Color object or hex string WITH OR WITHOUT '#' 
     }, 
     window: { 
      position: { 
       x: 'screenCenter', // acceptable values "left", "center", "right", "screenCenter", or relative px value 
       y: '200px', // acceptable values "top", "bottom", "center", or relative px value 
      }, 
      expandable: true 
     }, 
    }) 
}); 

});

+0

沒有代碼是正確的..你已經更新的代碼是正確的工作正常,我可以有多個選擇器與不同的ID ..我想設置後初始化另一種顏色 –

+0

請提供一個JSFiddle與代碼然後。 –

+0

嗯,問題是很清楚的:我初始化一個jPicker後,我用另一個ID initalital其他jPicker,如果我點擊一個按鈕,我想爲一個特定的jPicker設置一個新的顏色.. Jfiddle isn0t必須的,我只想要要知道是否有可能通過Id找到jPicker並將其顏色設置爲單擊按鈕 –

0

設定值

var control= $('#txtBackgroundColor') 
var colorPicked = '#e2ddcf'; 
control.jPicker({ 
    color: { active: colorPicked} 
}); 
control[0].color.active.val('hex', colorPicked); 
control.val(colorPicked.replace("#", "")); 

您的方案的一些線路的依賴上面可能是無關緊要的。

查找ID如果:

$('#txtBackgroundColor') 

是不正確的外觀與jPicker類的跨度上像這樣

<span class="jPicker"> 

只是上面,應該是與顯示器的輸入沒有ID值的風格。在上面的例子中使用那個。