Open browser-standard colorpicker with javascript without type=color

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 16:35:29

问题


Does anyone knows a javascript only way to open the browser-standard colorpicker, without using a html field? so i want a javascript what does exactly the same a a click on the html input color field. Bart


回答1:


You are going to have to use the input field, you can just hide it off the page. Issue here is the fact that the color dialog requires a click in browsers in order to open up the color dialog. It will not work if you just call click()

document.getElementById("xxx").addEventListener("click", function() {
  document.getElementById("c").focus();
  document.getElementById("c").value = "#FFCC00";
  document.getElementById("c").click();
});
.hidden {
  position: absolute;
  left: -10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
<input type="color" id="c" tabindex=-1 class="hidden">
<input type="button" id="xxx" value="Click Me!">



回答2:


Here's a good old "hover-hack" solution that works even in MS Edge:

<input type="color" style='opacity:0;width:100px;position:absolute;'/>
<button>clickme</button>

then bind onchange to the colro element

https://jsfiddle.net/Lnzm0sry/2/



来源:https://stackoverflow.com/questions/29676017/open-browser-standard-colorpicker-with-javascript-without-type-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!