【问题标题】:how to use multiple callbacks in jpicker如何在 jpicker 中使用多个回调
【发布时间】:2012-11-30 16:43:18
【问题描述】:

我想用jpicker修改多个元素,这是单个元素的代码(#body02),是否可以为每个元素创建一个数组?

   $(document).ready(
function()
{
  var LiveCallbackElement = $('#body02'),
      LiveCallbackButton = $('#LiveButton');  // you don't want it searching this

                                     // on every live callback!!!
  $('#Callbacks').jPicker(
    {window:{position:{x:'screenCenter',y:'200'}}},
    function(color, context)
    {
      var all = color.val('all');
      alert('Color chosen - hex: ' + (all && '#' + all.hex || 'none') +
        ' - alpha: ' + (all && all.a + '%' || 'none'));
      $('#body02').css(
        {
          backgroundColor: all && '#' + all.hex || 'transparent'
        }); // prevent IE from throwing exception if hex is empty
    },
    function(color, context)
    {
      if (context == LiveCallbackButton.get(0)) alert('Color set from button');
      var hex = color.val('hex');
      LiveCallbackElement.css(
        {
          backgroundColor: hex && '#' + hex || 'transparent'
        }); // prevent IE from throwing exception if hex is empty
    },
    function(color, context)
    {
      alert('"Cancel" Button Clicked');
    });      
  $('#LiveButton').click(
    function()
    {
      $.jPicker.List[0].color.active.val('hex', 'e2ddcf', this);
    });

});

谢谢

【问题讨论】:

  • 不清楚你想做什么,(a)单个选择改变几个元素的 bg-color 或(b)几个元素中的每一个都可以单独选择颜色?

标签: jquery color-picker jquery-callback jpicker


【解决方案1】:

有点晚了,但可能对其他人有帮助。如果您想为多个元素设置具有相同值的相同属性,只需添加类 id(逗号分隔)。

    $('#Id1, .class1, .class2, #AnotherId').css(
    {
      backgroundColor: all && '#' + all.hex || 'transparent'
    });

但如果您想设置单独的属性或使用单独的值,请进行单独的调用。

    $('#Id1').css(
    {
      backgroundColor: all && '#' + all.hex || 'transparent'
    });



    $('.someClass').css(
    {
      color: all && '#' + all.hex || 'transparent'
    });

由于 .css 需要一个 css 值数组,您还可以在同一个调用中对同一个元素应用多个更改。

    $('#Id1').css(
    {
      //Note the trailing comma except the last entry
      backgroundColor: all && '#' + all.hex || 'transparent',
      color: all && '#' + all.hex || 'transparent'
    }); 

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2017-03-13
    • 2012-07-02
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多