如何更改JavaScript变量的字体? [英] How do I change the font of a javascript variable?

查看:103
本文介绍了如何更改JavaScript变量的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作类似于曲奇饼发球台的游戏。这是我的代码的一部分:



var clicks = 0; function updateClickCount( ){document.getElementById(clickCount)。innerHTML = clicks; }

< center> < div class =game-object> < button type =buttononClick =clicks ++; updateClickCount(); id =pushstyle =width:400px; height:60px;> < font size =5face =verdanacolor =white>点击我可乐!< / font> < /按钮> < div id =clickCount>< / div> < / DIV> < / div>



我想让出现的变量在点击按钮后在屏幕上显示不同的字体。我也想知道如何使它变成不同的颜色。 解决方案

这是一个解决方案, 。我已经将内联javascript中的html更改为使用eventListener,这就是您想要在现实生活中使用的内容。

  var clicks = 0; document.getElementById(push)。addEventListener(click,updateClickCount); function updateClickCount(){clicks ++; var el = document.getElementById(clickCount); el.innerHTML =点击; Math.floor(Math.random()* 16777215).toString(16);}  

 < button type =buttonid =push>点击我可乐!< / font>< / button>< div id =clickCount>< / div>  

$ b

:通过 Paul Irish 创建随机颜色


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

 var clicks = 0;
function updateClickCount() {
  document.getElementById("clickCount").innerHTML = clicks;
 }

<center>
  <div class="game-object">    
  <button type="button" onClick="clicks++;updateClickCount();" id="push" style="width:400px;height:60px;">
    <font size="5" face="verdana" color="white">Click me for Cola!</font>
   </button>
   <div id="clickCount"></div>      
   </div>
  </div>

I want to make the variable that appears on the screen after clicking the button a different font. I also want to know how to make it different colors.

解决方案

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

这篇关于如何更改JavaScript变量的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆