Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改文本字段中的文本颜色_Javascript_Html_Css - Fatal编程技术网

Javascript 更改文本字段中的文本颜色

Javascript 更改文本字段中的文本颜色,javascript,html,css,Javascript,Html,Css,我有一个输入文本字段,默认情况下它有一个值“something”,但是当我开始键入时,我希望默认值改变颜色,我要键入的文本是另一个 我该怎么做 您可能需要尝试以下操作: <input type="text" value="something" onFocus="if (this.value == 'something') this.style.color = '#ccc';" onKeyDown="if (this.value == 'something')

我有一个输入文本字段,默认情况下它有一个值“something”,但是当我开始键入时,我希望默认值改变颜色,我要键入的文本是另一个

我该怎么做



您可能需要尝试以下操作:

<input type="text" value="something"
       onFocus="if (this.value == 'something') this.style.color = '#ccc';"
       onKeyDown="if (this.value == 'something') {  
                      this.value = ''; this.style.color = '#000'; }"> 

要使其像您的示例一样简单:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" />


这应该可以做到。

根据@chibu的回答,这就是使用jQuery和不引人注目的Javascript的方法


 
$(document).ready(
   function() {
      $("#mytext").bind(
         "click",
         function() {
            $(this).val("");
            $(this).css("color", "red");
         }
      );
   }
)
我们开始:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" />

祝你好运

继续编码

/“run”是按钮的id,“color”是输入标签的id
//代码从这里开始
(功能(){
const button=document.getElementById(“运行”);
按钮。addEventListener(“单击”,颜色更改);
函数colorChange(){
document.body.style.backgroundColor=document.getElementById(“颜色”).value;
}
})();

我想您指的是输入文本框?按钮中不能键入文本。谢谢您的回答,但是线程所有者在他们的要求中没有提到按钮。
<input type="text" value="something" onclick="this.value='';this.style.color='red';" />