繁体   English   中英

使用颜色选择器使用 jquery 更改颜色?

[英]changing colors with jquery with a color picker?

我有这个 jquery 脚本,它通过在文本框中输入颜色代码来立即更改背景的颜色,工作示例在下面的 jsfiddle 链接中。

http://jsfiddle.net/7jg4e/

但是我想使用颜色选择器自定义皮肤而不是输入字段,因为我不希望用户处理十六进制代码(就像 Twitter 一样)。 我尝试使用的 jquery 插件位于

http://www.eyecon.ro/colorpicker/

在页面的底部,它有一个带有 DOM 元素的整洁的颜色选择器。

所以问题是我将如何从输入类型更改为颜色选择器 div。 谢谢 :))

作为进一步参考... HTML5 已经包含“颜色”作为输入类型。

<label for="bg">Choose color:</label> 
<input id="bg" type="color" />

另外,您可以应用一些 css 样式:

input[type="color"]{/*css-here*/}

现在,对于主要问题……您可以使用简单的脚本捕获颜色值以更改 bg-color。 现场示例: http : //jsfiddle.net/7jg4e/152/

用 div 替换输入元素,使用如下内容:(未经测试!)

HTML

<div id='colourPicker'></div>

JS

$('#colourPicker').ColorPicker({
  onChange: function(hsb, hex, rgb){
    $("#full").css("background-color", '#' + hex);
  }
});

您拥有的链接底部有一个示例,向您展示了如何操作。

更新更改文本

HTML

<div id='colourPickerText'></div>
<div id='textToBeChanged'>Test text</div>

JS

$('#colourPickerText').ColorPicker({
  onChange: function(hsb, hex, rgb){
    $("#textToBeChanged").css("color", '#' + hex);
  }
});

怎么样:

$('#colorSelector').ColorPicker({
onChange: function(hsb, hex, rgb)
          {
            $("#full").css("background-color", hex);
          }
});

谢谢凯西斯..

我有类似的问题。 我不得不为动态按钮调用颜色选择器。 我做了上课而不是身份证。

这对我帮助很大。

$('.colorSelector').ColorPicker({
onChange: function(hsb, hex, rgb,el)
      {
        $(el).val('#' + hex);
      }
});

我有同样的问题,下面是我的解决方案。

编辑 colorpicker.js 第 96 行

cal.data('colorpicker').onChange.apply(cal, [col, HSBToHex(col), HSBToRGB(col)]);

cal.data('colorpicker').onChange.apply(cal, [col, HSBToHex(col), HSBToRGB(col), cal.data('colorpicker').el]);

on change 事件而不是 onChange: function (hsb, hex, rgb) ...

onChange: 函数 (hsb, hex, rgb,el) {
$(el).val('#' + hex);
}

 
 
let drawingBuffer
      getColor(colorPicker) {
        let color = 'ColorPicker1';
        this.drawingBuffer = document.getElementsByClassName("colorSets");
         for (var i = 0; i < this.drawingBuffer.length; i++) {
             this.drawingBuffer[i].style.color = colorPicker;
         }
         this.drawingBuffer.style.color = colorPicker;
        return color;
    }
<div class="colorSets">test</div>
<div class="colorSets">test1</div>
<input name="MyColorPicker" id="ColorPicker1" [cpPresetLabel]="color" [(colorPicker)]="color" [style.background]="color" (colorPickerChange)="getColor($event)"/>


//this is not working in snipest but use as it code and then use npm install it will work for you

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM