Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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 向搜索框中的Onfocus事件添加字体颜色_Javascript_Html - Fatal编程技术网

Javascript 向搜索框中的Onfocus事件添加字体颜色

Javascript 向搜索框中的Onfocus事件添加字体颜色,javascript,html,Javascript,Html,如何在我的搜索框中添加默认值的颜色和字体 <input type="text" name="q" value="Enter your keywords here" onfocus="if (this.value == 'Enter your keywords here') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter your keywords here';}" onwebkitsp

如何在我的搜索框中添加默认值的颜色和字体

<input type="text" name="q" value="Enter your keywords here"    
onfocus="if (this.value == 'Enter your keywords here') {this.value = '';}" onblur="if (this.value   == '') {this.value = 'Enter your keywords here';}"
onwebkitspeechchange = "this.value = this.value.replace('Enter your keywords here','')";
x-webkit-speech style="left:9px;top:4px;position:relative;background-color:#fafafa;border:1px     solid;color:#333;font-size:1.4em;width:400px;height:28px"; />

您可以设置输入框的默认样式,然后按如下方式更改onfocus和onblur事件的颜色:

<input type="text" name="q" value="Enter your keywords here"    
    onfocus="if (this.value == 'Enter your keywords here') {
        this.value = ''; 
        this.style.color = '#000';
    }" 
    onblur="if (this.value == '') {
        this.value = 'Enter your keywords here'; 
        this.style.color = '#CCC';
    }"
    onwebkitspeechchange = "this.value = this.value.replace('Enter your keywords here','')";
    x-webkit-speech 
    style="left:9px;top:4px;position:relative;background-color:#fafafa;border:1px solid; color:#CCC;font-size:1.4em;width:400px;height:28px"; 
/>

你应该考虑把这个事件代码放在一个单独的脚本中使代码看起来更干净。 你也可以看看这个小提琴,看看更简洁的代码。


更改样式中的颜色

<input type="text" name="q" value="Enter your keywords here"    
onfocus="if (this.value == 'Enter your keywords here') {this.value = '';}" onblur="if (this.value   == '') {this.value = 'Enter your keywords here';}"
onwebkitspeechchange = "this.value = this.value.replace('Enter your keywords here','')";
x-webkit-speech style="left:9px;top:4px;position:relative;background-color:#fafafa;border:1px solid     #555;color:#aaa;font-size:1.4em;width:400px;height:28px"; />