为了账号安全,请及时绑定邮箱和手机立即绑定

页面加载时如何更改 <input type="color"/> 的颜色值?

页面加载时如何更改 <input type="color"/> 的颜色值?

侃侃尔雅 2023-10-23 10:48:53
我正在尝试制作一个网页,允许用户通过颜色输入选择颜色,并且该页面应将他们的选择存储在 localStorage 中。理想情况下,用户下次打开页面时仍然应该能够查看他们制作的调色板。我的问题是颜色输入的值不会随我的 javascript 代码更新。我尝试编写window.onload,甚至在标签中插入“onload()”。他们都没有工作。输入仍然显示相同的默认颜色值,即纯黑色。我确实需要一些帮助来解决这个问题。非常感谢~这是我的 HTML 代码:<body onload="getColor()">    <div class="container">        <div id="mySidebar" class="sidebar">            <label class="noteLabel">C</label>            <input type="color" id="color0" name="color" onchange="handleChange(this.value, this.id)" />            <label class="noteLabel">F#</label>            <input type="color" id="color6" name="color"                onchange="handleChange(this.value, this.id)" /><br />            <label class="noteLabel">C#</label>            <input type="color" id="color1" name="color" onchange="handleChange(this.value, this.id)" />            <label class="noteLabel">G</label>            <input type="color" id="color7" name="color"                 onchange="handleChange(this.value, this.id)" /><br />            <label class="noteLabel">D</label>            <input type="color" id="color2" name="color" onchange="handleChange(this.value, this.id)" />            <label class="noteLabel">G#</label>            <input type="color" id="color8" name="color"                 onchange="handleChange(this.value, this.id)" /><br />            <label class="noteLabel">D#</label>            <input type="color" id="color3" name="color" onchange="handleChange(this.value, this.id)" />            <label class="noteLabel">A</label>            <input type="color" id="color9" name="color"                 onchange="handleChange(this.value, this.id)" /><br />            <label class="noteLabel">E</label>
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

我刚刚发现我的代码出了什么问题。事实证明,rgbToHex()我定义的函数无法正常工作,因此收到的值输入元素的格式不正确。直到现在我才注意到这一点,因为控制台只提到这个问题作为警告。



查看完整回答
反对 回复 2023-10-23
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

请使用value代替defaultValue. 看下面的代码:


document.getElementById("color11").value = "#ff0000";
<input type="color" id="color11" name="color"
                 onchange="handleChange(this.value, this.id)" />


查看完整回答
反对 回复 2023-10-23
  • 2 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信