Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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_Google Search - Fatal编程技术网

Javascript 使用谷歌自定义搜索清除焦点上的输入

Javascript 使用谷歌自定义搜索清除焦点上的输入,javascript,google-search,Javascript,Google Search,我正在将一个站点搜索转移到谷歌自定义搜索 文本的旧输入如下所示: <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...

我正在将一个站点搜索转移到谷歌自定义搜索

文本的旧输入如下所示:

<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'name'设置为'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时,我无法清除焦点上的搜索框。我是否遗漏了一些非常简单的内容?

我不知道为什么会发生这种情况,但我建议您不要使用JavaScript来完成这项工作,而是让HTML通过占位符属性完成这项工作:

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

您是否也包括CSE JS?它很可能会覆盖你的焦点和模糊处理程序。页面顶部添加了谷歌品牌的水印

您可以在没有JS的情况下实现CSE,在这种情况下,您的代码可以正常工作:

<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>

哈哈,我希望全世界都会:|你也需要一个HTML5 doctype。请看我在上的帖子,有趣的是,它的排名比公认的答案高出3。在那之后,回答者改变了他的回答,支持我的回答——对信息保存来说很可怕,但这是一个很好的姿态。