简体   繁体   English

根据用户输入更改文本/字体颜色

[英]Change text/font colour based on user input

I'm trying to build some JavaScript code that detects the users input in a textbox. 我正在尝试构建一些JavaScript代码,以检测用户在文本框中的输入。

If the user enters, for example, the word 'test', I want the text/font colour to change to green. 例如,如果用户输入单词“ test”,我希望文本/字体颜色变为绿色。

Any other text that the user enters (such as 'tes', 'test1', 'testlm', 'biscuit', etc.) should change the text/font colour to red. 用户输入的任何其他文本(例如“ tes”,“ test1”,“ testlm”,“ biscuit”等)应将文本/字体颜色更改为红色。

I've tried multiple ways of doing this with no avail. 我已经尝试了多种方法,但都无济于事。 Please note that I am an absolute beginner with JavaScript and I am 'playing around' with code to help me learn it. 请注意,我绝对是JavaScript的初学者,我正在玩代码以帮助我学习。 Therefore, if the code is messy or completely wrong, I apologise for my lack of knowledge. 因此,如果代码混乱或完全错误,对于我的知识不足,我深表歉意。

Here is the JavaScript code for the first test: 这是第一个测试的JavaScript代码:

<script type="text/javascript">
function checkKey() {
var plaintext = document.getElementById("textbox");
var correct = input.style.color = '#66CD00';
var incorrect = input.style.color = '#FF0000';

if(plaintext =='test')
    {
       document.getElementById("textbox").innerHTML = correct;
}
else 
   {
       document.getElementById("textbox").innerHTML = incorrect;
   }

};
</script>

And the HTML code for the textbox: 以及文本框的HTML代码:

<input type="text" id="textbox" name="plaintext" onKeyDown="checkKey()">

The second test is the same as the first, with the only difference in the JavaScript code being the 'var correct' and 'var incorrect' parts: 第二个测试与第一个测试相同, JavaScript代码中唯一的区别是“正确”和“错误”部分:

var correct = str.fontcolor("green");
var incorrect = str.fontcolor("red");

Both tests didn't work. 两项测试均无效。 I even took away the brackets in the HTML textbox code for the onKeyDown attribute for both tests: 我什至没有为两个测试的onKeyDown属性删除HTML文本框代码中的括号:

<input type="text" id="textbox" name="plaintext" onKeyDown="checkKey">

Which again didn't work. 这又没有用。

I was wondering if there's any way to achieve my aforementioned desired outcome? 我想知道是否有任何方法可以实现上述期望的结果? Have I done anything right in my experimental code? 我在实验代码中做了正确的事情吗?

Thanks in advance for your time (and sorry for the long question). 在此先感谢您的宝贵时间(对于较长的问题,深表歉意)。

Here is an example to complete your initial request. 这是一个完成初始请求的示例。 I will edit this answer in a few to give you some pointers regarding your code. 我将对此答案进行一些编辑,以便为您提供有关代码的一些提示。

https://jsfiddle.net/h05hgf3g/2/ https://jsfiddle.net/h05hgf3g/2/

JS JS

function checkKey() {
 var plaintext = document.getElementById("textbox"); 
 if(plaintext.value != 'test') {
  plaintext.style.color = "#FF0000";
 }
 else {
   plaintext.style.color = "#66CD00";
 }
}

HTML HTML

<input type="text" id="textbox" name="plaintext" onKeyUp="checkKey()">

Regarding your code: 关于您的代码:

<script type="text/javascript">
function checkKey() {
var plaintext = document.getElementById("textbox");

Here you declare plaintext as the object with the Id textbox . 在这里,您使用Id textbox将纯文本声明为对象。 Later on, you use the whole document.getElementById command again also for textbox . 稍后,您也将整个document.getElementById命令也用于textbox Since you have put this object into the variable plaintext , you can just refer back to it as plaintext moving forward. 由于已将该对象放入变量plaintext ,因此可以将其称为前进的plaintext Keep in mind variables declared inside of a function only live inside that function if they are declared with var . 请记住,在函数内部声明的变量只有在使用var声明时才存在于该函数内部。

var correct = input.style.color = '#66CD00';
var incorrect = input.style.color = '#FF0000';

You are not able to use = twice in a statement like above 您不能在上述语句中两次使用=

if(plaintext =='test')

plaintext is a variable which is currently storing the input object as whole. plaintext是当前存储整个输入对象的变量。 If you check it's value at the highest level you will see that is it: [Object HTML INPUT Element] . 如果在最高级别检查它的值,将会看到它是: [Object HTML INPUT Element] Therefore, the object itself will never equal anything besides that right there. 因此,对象本身永远不会等于那里的那个东西。 If you are looking for the value that this object is currently retaining, we use .value . 如果要查找该对象当前保留的value ,则使用.value

    {
       document.getElementById("textbox").innerHTML = correct;

Here is where we can reuse our plaintext variable instead of calling document.getElementById again. 在这里,我们可以重用我们的plaintext变量,而不用再次调用document.getElementById So we switch that to plaintext and then we want to assign the value of this object a different color. 因此,我们将其切换为plaintext ,然后要为该对象的value分配不同的颜色。

}
else 
   {
       document.getElementById("textbox").innerHTML = incorrect;
   }

};
</script>

In Summary: 综上所述:

You are off to a good start and if you wrote that code you posted, though it was technically inaccurate, it does show signs of trying to figure out solutions on how to do things. 您已经有了一个良好的开端,并且如果您编写的是发布的代码,尽管从技术上讲它是不准确的,但它确实显示出试图找出解决方案的迹象。 From here, there are all sorts of things you can do with this tiny example to expound on it and make it much more dynamic. 从这里开始,您可以使用这个小例子做各种各样的事情,以阐明它并使之更加动态。

Possible things to add on/think about: 需要补充/考虑的可能事情:

  • What happens when a user writes 'Test' instead of 'test' 用户写“测试”而不是“测试”时会发生什么
  • What if you have 4 input boxes that you want to follow all the same coloring rules? 如果您有4个输入框要遵循所有相同的着色规则,该怎么办? Do you need to make 4 separate functions, 1 for each? 您需要制作4个单独的功能,每个功能1个吗? Or can you re-use the same single function to manage all 4 inputs? 还是可以重复使用同一功能来管理所有4个输入?
  • What about having the user select a color he/she chooses? 让用户选择他/她选择的颜色怎么样?
<input type="text" id="textbox" name="plaintext" onkeyup="checkKey();">
<script>
function checkKey() {
    if(document.getElementById("textbox").value ==='test')
    {
       document.getElementById("textbox").style.color = "#00FF00";
    }
    else 
    {
       document.getElementById("textbox").style.color = "#FF0000";
    }
}
    </script>

Check this code. 检查此代码。

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

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