[英]Get instance to call method?
I'm currently trying to set the value of a colorpicker. 我目前正在尝试设置颜色选择器的值。 I'm using this color picker: http://mjolnic.github.io/bootstrap-colorpicker/
我正在使用这个颜色选择器: http : //mjolnic.github.io/bootstrap-colorpicker/
The colorpicker is bound to a html element with jquery: 使用jquery将colorpicker绑定到html元素:
$('.sample-selector').colorpicker({ /*options...*/ });
It also has a few methods like: 它还有一些方法,例如:
.setColor(value)
How can i get the colorpicker instance so i can call a method? 如何获取colorpicker实例,以便可以调用方法?
I tried a lot of things. 我尝试了很多事情。 First i init colorpicker:
首先我初始化colorpicker:
$('#thecolor').colorpicker();
This works. 这可行。 But i can't seem to find a way to call a method:
但是我似乎找不到找到方法的方法:
$('#thecolor').colorpicker.toRGB()
$('#thecolor').toRGB()
$('#thecolor').colorpicker().toRGB()
and so on. 等等。 It must but something really small :)
它必须但确实很小:)
To get the colorpicker instance from the DOM: 要从DOM获取colorpicker实例:
var cp = $('#thecolor').data().colorpicker;
The color object is a property: 颜色对象是一个属性:
cp.color.toRGB();
From the bootstrap-colorpicker
doc, you can call toRGB
like this: 从
bootstrap-colorpicker
文档中,您可以像这样调用toRGB
:
$('#thecolor').colorpicker().on('changeColor', function(ev){
console.log(ev.color.toRGB());
});
I think you've missed this line: 我认为您错过了这一行:
Color object methods
颜色对象方法
Each triggered events have a color object used internally by the picker.
每个触发的事件都有一个颜色对象,供选择器内部使用。 This object has several useful methods.
该对象有几种有用的方法。
Look at the following example: 看下面的例子:
$('.my-colorpicker-control').colorpicker().on('changeColor', function(ev){
bodyStyle.backgroundColor = ev.color.toHex();
});
So it's not available from the outside. 因此无法从外部使用。 You can try to hack around that (not tested)
您可以尝试破解(未经测试)
$('.my-colorpicker-control').colorpicker().on('create', function(ev) {
window.mycolorpicker = ev.color;
});
if (window.mycolorpicker) {
window.mycolorpicker.setColor('...');
}
Good luck! 祝好运!
Try this: 尝试这个:
var colorPicker = $("#thecolor").colorpicker();
$(colorPicker).toRGB(); // object method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.