[英]Trying to change the color of a button onclick
我對開發非常陌生,因此請原諒我缺乏知識。 我正在嘗試更改正在制作的頁面上的按鈕的背景色。 我創建了類似的東西。
在此示例中,我想將日期打開時將背景色更改為紅色,將日期關閉將背景色更改為綠色。
請在這里解釋我做錯了什么
<!doctype>
<html>
<body>
<h1>My First Button in Java Script.</h1>
<p>Please click the button to display the date.</p>
<!=========this is button=============>
<button type="button" onclick="myFunction()" id="on" style=background-color:#32CD32>Turn on Date</button>
<p id="demo"></p>
<!==== this is the button that will show the date====>
<script>
function myFunction(){
var x = document.getElementById("demo");
var onoff= document.getElementById("on");
if (x.innerText=="")
{
x.innerHTML = Date(); n = "Turn Date Off"; onoff.bgcolor="#FF0000"}
else {
x.innerHTML =""; n = "Turn Date On"; onoff.bgcolor= "#32CD32"}
onoff.innerText = n; }
</script>
</body>
</html>
請注意,我從W3schools復制了此代碼,然后對其進行了編輯。
您只需要更改以下顏色的語法即可:
onoff.bgcolor = "#FF0000"
onoff.bgcolor = "#32CD32"
至:
onoff.style.backgroundColor = "#FF0000"
onoff.style.backgroundColor = "#32CD32"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.