2010-05-20 48 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