javascript 如何使用javascript获取type="color"输入的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23686180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:23:15  来源:igfitidea点击:

how to get value of type="color" input using javascript

javascripthtml

提问by user3624843

I used this above html code for my project..but i don't know how to get value of above input using javascript

我在我的项目中使用了上面的 html 代码..但我不知道如何使用 javascript 获取上述输入的值

<form>
  <input type="color" id="favcolor">
</form> 

can someone help me ?

有人能帮我吗 ?

Thanks

谢谢

回答by Yaaso

To simply get the value

简单地获得价值

document.getElementById("favcolor").value;

You can add an event listener if you want to get the color when the selection changes. You'd do something like this

如果要在选择更改时获取颜色,可以添加事件侦听器。你会做这样的事情

   var theInput = document.getElementById("favcolor");
   var theColor = theInput.value;
   theInput.addEventListener("input", function() {

   <<Do something with theColor value here>>

    }, false);

Here's a working example: http://jsfiddle.net/3fehj/

这是一个工作示例:http: //jsfiddle.net/3fehj/