简体   繁体   English

仅使用Javascript / HTML单击时如何更改按钮的文本和颜色

[英]How to change text and color of a button when it is clicked using Javascript/HTML only

I need to be able to change the a button color and text to something other than the default. 我需要能够将按钮的颜色和文本更改为默认以外的其他颜色。

<button id="partyTimeButton">Party Time!</button>

JS JS

var partyEvent = function()
{
    if (partyBtn < 0) 
    {
        partyBtn = new Date().getHours(); 
        partyBtn = "Party Over";

      // text in the button should read "Party Over"
        // color of the button should be "#0A8DAB" (bonus!)
    }

    else
    {
        partyBtn = -1;
        partyBtn = "PARTY TIME!"; 
        // text in the button should read "PARTY TIME!"
        // color of the button should be "#222" (bonus!)
    }
};

partyBtn.addEventListener("click", partyEvent);

Your code snippet has some issues with the way you defined variables and text. 您的代码段在定义变量和文本的方式上存在一些问题。 PartyBtn is updated as -1 and "Party time!" PartyBtn更新为-1和“ Party time!”。 and as hours number and "Party Over" , one after the other, which I think, shouldn't be. 作为小时数和“ Party Over”,我认为不应该一个接一个。

Updated the snippet with some formatting and variables declarations. 使用一些格式和变量声明更新了代码段。 Hope this should get you going. 希望这可以帮助您前进。

 var partyBtn = document.querySelector("#partyTimeButton"); var partyBtnCount = -1; var partyEvent = function() { console.log(partyBtnCount); if (partyBtnCount < 0) { partyBtnCount = new Date().getHours(); partyBtn.innerHTML = "Party Over"; partyBtn.style.color = "#0A8DAB" // text in the button should read "Party Over" // color of the button should be "#0A8DAB" (bonus!) } else { partyBtnCount = -1; partyBtn.innerHTML = "PARTY TIME!"; partyBtn.style.color = "" // text in the button should read "PARTY TIME!" // color of the button should be "#222" (bonus!) } }; partyBtn.addEventListener("click", partyEvent); 
 <button id="partyTimeButton">Party Time!</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改文本的颜色,并在javascript中单击4次按钮时停止 - How to change color of text and stop when clicked the button with 4 times in javascript Javascript 单击时如何更改按钮的颜色? - Javascript how to change the color of a button when clicked? 我需要使用 HTML、CSS 和 Javascript 制作一个按钮,以便在单击按钮时将字体颜色从红色变为红色 - I need to make a button using HTML, CSS, and Javascript to change font color from to red when the button is clicked <button>在HTML中单击时更改颜色?</button> - <button> change color when clicked in HTML? Javascript:<a>单击按钮时如何更改标签的颜色</a> - Javascript: how to change <a> tag's color when button is clicked 单击检查答案按钮时如何使用 JavaScript 更改正确答案的颜色 - How to change the color of correct answer using JavaScript when check answer button clicked 单击时如何将html文本更改为按钮的值? - How to change html text to value of button when clicked? 在 HTML 中使用 JavaScript 单击时如何切换按钮文本 - How to toggle button text when clicked with JavaScript in HTML 在 JavaScript 中单击提交按钮时如何更改段落的文本 - How to change text of a paragraph when submit button is clicked in JavaScript 使用JavaScript单击时更改按钮上的文本 - Change text on button when clicked with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM