How do I change the font of a javascript variable?

前端 未结 3 1236
有刺的猬
有刺的猬 2021-01-24 06:05

I am trying to make a game similar to cookie clicker. This is part of my code:

相关标签:
3条回答
  • 2021-01-24 06:41

    You can add a function that is called on click which can update your css. Try this

    <button onclick="myFunction()"> Click me </button>
    
    
     function myFunction()
        {
            document.getElementById('clickCount').setAttribute("class", "style1");
        }
    

    Here is some more info on .setAttribue: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

    0 讨论(0)
  • 2021-01-24 06:53

    Add some CSS to style the element:

    <style>
        #clickCount {
            color: red;
            font-family: Gill Sans Extrabold, sans-serif;
        }
    </style>
    
    0 讨论(0)
  • 2021-01-24 07:01

    Here's a solution giving your number a random new color on every click. I've changed the html from inline javascript to using an eventListener, which is what you want to use in real life

    var clicks = 0;
    
    document.getElementById("push").addEventListener("click", updateClickCount);
    
    function updateClickCount() {
    	clicks++;
    	var el = document.getElementById("clickCount");
      el.innerHTML = clicks;
      el.style.color = '#'+Math.floor(Math.random()*16777215).toString(16);
    }
    <button type="button" id="push">Click me for Cola!</font></button>
    <div id="clickCount"></div>

    Attribution: random color creation by Paul Irish

    0 讨论(0)
提交回复
热议问题