简体   繁体   English

JavaScript颜色选择器问题

[英]Javascript color picker issue

I am building a simple paint app in HTML5 and JS. 我正在用HTML5和JS构建一个简单的绘画应用程序。 I am having trouble changing the color of the canvas background. 我无法更改画布背景的颜色。

I have an input box that calls the color picker. 我有一个称为颜色选择器的输入框。 When the value changes it should call the getBgColor() function and update the canvas. 当值更改时,应调用getBgColor()函数并更新画布。 However the function is not even being executed and I have no idea why!! 但是该功能甚至没有被执行,我也不知道为什么!

Input Box: 输入框:

 <input id = "bgcolor_select" type="text" value="Background Color" onclick="colorPicker(event)" onchange="getBgColor()"/>

Function called by Input Box when value changes: 值更改时,由输入框调用的函数:

function getBgColor(){
    console.log("Bg Color: " + document.getElementById("bgcolor_select").value);
    context.fillStyle = document.getElementById("bgcolor_select").value;
    context.fillRect(0,0, canvas.width, canvas.height);
    }

When I run my application I can see the value changing in the field as I select colors but for some reason it does not call the function. 当我运行我的应用程序时,我可以在选择颜色时看到字段中的值发生变化,但是由于某种原因它没有调用该函数。 If I add a character to the value and remove it, then it calls the function. 如果我在值中添加一个字符并将其删除,则它将调用该函数。

Any help appriciated! 任何帮助!

When setting input.value = ... , no change event is fired. 设置input.value = ... ,不会触发change事件。 This only happens when the user changes the value through the actual textbox. 仅当用户通过实际文本框更改值时,才会发生这种情况。 You can manually fire it yourself, but in this case you can just call getBgColor() instead. 您可以自己手动触发它,但是在这种情况下,您可以只调用getBgColor()

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

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