我正在嘗試編寫一個需要使用按鈕更改背景的網(wǎng)站,但我無法...我創(chuàng)建了一個按鈕,可以在單擊按鈕時更改背景顏色,但是在添加圖像作為背景后,它不想更改背景顏色(顯然)...<head><style>:root{ --main-bg: url(https://www.the url of my bg.jpg);}</style> // creating a variable for the bg of my site<style>body { background: var(--main-bg) repeat-x;}</style> // using this variable<script> var colors = ["#2d2d2d", "#f2f2f2"]; var colorIndex = 0; function changeColor() { var col = document.getElementById("body"); if( colorIndex >= colors.length ) { colorIndex = 0; } col.style.backgroundColor = colors[colorIndex]; colorIndex++;// the js script to change the bg color } </script><script> function backgroundHiding() { let root = document.documentElement; root.addEventListener("onclick", e => { if (--main-bg) = "#2d2d2d" { root.style.setProperty('--main-bg', " url(https://www.the urlof my bg.jpg)") } else { root.style.setProperty('--main-bg', "#2d2d2d") } // the js script to hide my img bg } </script><div class="button"> <body id='body'> <button onclick="changeColor(); backgroundHiding();" class="lightbutton"><span>Turn On/Off the lights</span> </button></div> // creating the button that interact with the two js functions</head>我預(yù)計它會起作用,但是......呃......不它有時有效,比如每 100 次 1 次,我不知道為什么......如果有人能幫助我,我會很感激!https://stackblitz.com/edit/typescript-ppvsvl?embed=1&file=index.html
如何使用按鈕更改網(wǎng)站的背景?
手掌心
2021-06-08 14:15:57