简体   繁体   English

满足条件时更改字体颜色

[英]changing font colour when a condition is met

does anyone know how I would do this within an HTML-5 web based application? 有谁知道我将如何在基于HTML-5的Web应用程序中执行此操作? ie I need to change the font colour of a name when a button is pressed. 即我需要在按下按钮时更改名称的字体颜色。 It's for an application that easily shows when players are on or off court in a tennis tournament. 它的应用程序可以轻松显示网球比赛中场上或场下的球员。 so far I have an excel document of all the players loaded in to an html, this is one of the names for instance: 到目前为止,我有一个将所有播放器加载到html的excel文档,例如,这是名称之一:

http://i.imgur.com/Iapdp.png

and buttons as follows: 和按钮如下:

http://i.imgur.com/kEGYm.png

so I assume I could write an if condition that states if that button is on, then the player's name goes red or something.. I'm also struggling with layout, considering the names are in a table, the button just stays above the table wherever I put it. 所以我假设我可以编写一个if条件,说明该按钮是否打开,然后玩家的名字变成红色或其他内容。.我也在努力布局,考虑到名字在桌子上,按钮一直在桌子上方无论我放在哪里。 Any help is much appreciated, Louis. 任何帮助都非常感激,路易斯。

You could simply add/remove a class instead of only changing the value. 您可以简单地添加/删除class而不是仅更改值。 This class can be referenced via CSS. 可以通过CSS引用此类。

Example

.online_text {
    color: #00ff00;
}
.offline_text {
    color: #ff0000;
}

Your script should add/remove the property so you basically get this format : 您的脚本应该添加/删除属性,这样您基本上可以得到以下格式

<td ... class="online_text">Oliver Addison</td>

Adding the class as example: 添加类作为示例:

document.getElementById("onoff").className += "online_text";

You need to give the element that you want to change something defining - usually an ID. 您需要为要更改的元素提供定义的定义-通常是一个ID。 For example, <td id="currentMatch"> 例如, <td id="currentMatch">

Note that IDs need to remain unique throughout your page. 请注意,在整个页面中,ID必须保持唯一。 You can then access it via document.getElementById("currentMatch") 然后,您可以通过document.getElementById("currentMatch")访问它

To set the color of the text you'd want to use 设置您要使用的文本的颜色

document.getElementById("currentMatch").style.color = "#00FF00"

So that's how you change the color. 这就是您更改颜色的方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM