2010-05-20 49 views
0

我正在将网站搜索移至Google自定义搜索。使用Google自定义搜索清除输入焦点

旧的输入文字是这样的:

<input type="text" value="Search this website..." name="s" id="searchbox" onfocus="if (this.value == 'Search this website...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search this website...';}" /> 

现在我需要有ATTR“名称”作为“Q”,像这样:

<input type="text" value="Search this website..." name="q" id="searchbox" onfocus="if (this.value == 'Search this website...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search this website...';}" /> 

奇怪的是,当我将名称换成q时,我无法清除焦点上的搜索框。我错过了超级简单的东西吗?

回答

1

你是不是也包括CSE JS?有可能会覆盖你的焦点和模糊处理程序。 default implementation(请参阅页面顶部)添加了Google品牌的水印。

您可以实现自定义搜索引擎没有他们的JS,而在这种情况下,你的代码工作正常:

<form action="http://www.google.com/cse" id="cse-search-box"> 
    <input name="cx" type="hidden" value="yoursearchid" /> 
    <input name="ie" type="hidden" value="UTF-8" /> 
    <input name="q" type="text" value="Search this website..." onfocus="if (this.value == 'Search this website...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search this website...';}" /> 
    <input name="sa" type="submit" value="Search" /> 
</form> 
-2

我不知道为什么会发生,但我建议你不要使用JavaScript来做到这一点,让HTML做的工作与placeholder属性:

<input type="text" placeholder="Search this website..." name="q" id="searchbox" /> 
+0

这只是产生一个空白输入框.. – wesbos 2010-05-20 18:12:16

+0

得到一个更好的浏览器:) – 2010-05-20 18:18:21

+0

哈哈,我希望全世界会:| – wesbos 2010-05-20 18:31:05