[英]How do I change the background color with javascript using the text within a button
/** HTML File **/
<button id="blue" onclick="colors()">blue</button>
/** JavaScript File **/
function colors() {
document.body.style.backgroundColor=
document.getElementById('blue').value;
}
“按鈕”包含文本“藍色”
我正在嘗試使背景顏色等於該文本。
改用innerHTML
:
function colors() {
document.body.style.backgroundColor = document.getElementById('blue').innerHTML;
}
<button onclick="colors('blue')">blue</button>
<script>
/**
** @param {String} color - The color you want to change to
**/
function colors(color) {
document.body.style.backgroundColor = color;
}
</script>
改用輸入
/** HTML File **/
<input type="button" id="blue" value="blue" onClick="colors();"></input>
/** JavaScript File **/
function colors() {
document.body.style.backgroundColor=
document.getElementById('blue').value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.