在搜索框中将字体颜色添加到Onfocus事件

时间:2012-06-02 05:14:05

标签: 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';}"
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"; />

2 个答案:

答案 0 :(得分:3)

您可以设置输入框的默认样式,然后更改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"; 
/>

您一定要考虑将此事件代码放在一个单独的脚本中,以使您的代码看起来更清晰。

您还可以查看这个小提琴,获得更清晰的代码。 http://jsfiddle.net/MXpSw/

答案 1 :(得分:0)

改变风格的颜色......

<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"; />