简体   繁体   English

改变输入值的颜色

[英]Changing color of input value

I have login script where are inputs email and password. 我有登录脚本,输入电子邮件和密码。 Email input value is set "email" but when It's not correct i added a script which change value to "email not correct" but i wanted it have color red. 电子邮件输入值设置为“电子邮件”,但是当它不正确时,我添加了一个脚本,将值更改为“电子邮件不正确”,但我希望它有红色。 Code below do not work. 下面的代码不起作用。 How do i change it? 我该如何改变它?

<input id="email" type="text" name="logemail" placeholder="Email">


echo '<span class=\'wrong\'>
      <script>
      document.getElementById("email").value = "incorrect email";
      </script>
      </span>';

CSS: CSS:

.wrong{
color:red;
}

Your CSS changes the value of text in the span element not the input value of the text box. CSS会更改span元素中文本的值而不是文本框的输入值。 Technically speaking, your current code only changes the colour of the "innerHTML" of the span element not the "value" of the input element. 从技术上讲,您当前的代码只会更改span元素的“innerHTML”的颜色而不是输入元素的“value”。

Change 更改

.wrong{
    color:red;
}

To

#email{
    color:red;
}

To get the desired behaviour... 为了获得理想的行为......

Use separate javascript function to add class dynamically. 使用单独的javascript函数动态添加类。

<script>
    function addClass(element, myClass) {
        element.className += ' ' + myClass;
}
</script>
<span class='wrong1'>
    <script>
        document.getElementById("email").value = "incorrect email";
        var foo = document.getElementById('email');
        addClass(foo, 'wrong');
    </script>
</span>

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

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