简体   繁体   English

JavaScript 字体颜色根据用户输入改变

[英]JavaScript font color change based on user input

so I have this little temperature converter.所以我有这个小温度转换器。 All I want is to change the temp font color to red when is higher than 30 (eg), and I can't to get this work.我想要的只是在高于 30(例如)时将临时字体颜色更改为红色,而我无法完成这项工作。 I would be obliged for any help.我将不得不提供任何帮助。

 function colorChange(myVal) { var myVal = document.getElementById("fahrenheit").value; if (myVal > 30) { document.getElementById("fahrenheit").style.color = "red"; } } colorChange();
 <input type="number" id="inputCelsius" placeholder="Enter Celsius" oninput="myFunction(this.value)" onchange="myFunction(this.value)" /> <p>Fahrenheit: <span id="fahrenheit"></span></p> <p>Kelvin: <span id="kelvin"></span></p>

You can use the onchage event to call the action when input is changed您可以使用onchage事件在输入更改时调用操作

 function hola(){ let elem = document.getElementById('my-id') if(elem.value > 30){ elem.style.color = 'red' }else{ elem.style.color = 'black' } }
 <input placeholder="whatever" id="my-id" onchange="hola()"></input>

If you use oninput the result will be the same, but the function will be triggered every time you type如果你使用oninput结果会是一样的,但是每次输入都会触发 function

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

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