1 回答

TA貢獻1796條經驗 獲得超7個贊
這似乎工作正常。也許您想要“輸入”事件而不是“更改”?
該input事件將立即更改它只有在您單擊顏色選擇器外部后,
該事件才會更改它change
如果你想使用這個顏色值,你可以簡單地將它分配給一個變量
context.strokeStyle = e.target.value;
document.getElementById('brushcolor').addEventListener('change', e => {
document.documentElement.style.setProperty('--brushcolor', e.target.value);
});
document.getElementById('brushcolor2').addEventListener('input', e => {
document.documentElement.style.setProperty('--brushcolor', e.target.value);
});
:root {
--brushcolor: #000000;
}
.variablebackground {
background-color: var(--brushcolor);
width: 200px;
height: 100px;
}
<html>
<body>
<div class="variablebackground"></div>
<div class="optioninputs">
<label for="brushcolor">change eventlistener</label>
<input id="brushcolor" type="color" name="brushcolor" value="#000000">
<br />
<label for="brushcolor2">input eventlistener</label>
<input id="brushcolor2" type="color" name="brushcolor2" value="#000000">
</div>
</body>
</html>
添加回答
舉報