使用 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 '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 时,我无法清除搜索框聚焦。我错过了一些超级简单的东西吗?
I'm moving a sites search over to google custom search.
The old input for text looks like this:
<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...';}" />
Now I need to Have the attr 'name' as 'q', like so:
<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...';}" />
The odd thing is that when I swap the name over to q, I'm unable to clear the search box on focus. Am I missing something super easy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你们还包括 CSE JS 吗?它很可能会覆盖您的焦点和模糊处理程序。 默认实现(请参见页面顶部)会添加 Google 品牌水印。
你可以在没有 JS 的情况下实现 CSE,在这种情况下你的代码可以正常工作:
Are you also including the CSE JS? Chances are it's overwriting your focus and blur handlers. The default implementation (see top of page) adds a Google-branded watermark.
You can implement CSE without their JS, and in that case your code works fine:
我不知道为什么会发生这种情况,但我建议您不要使用 JavaScript 来执行此操作,而让 HTML 使用
placeholder
属性来完成此工作:I don't know why that would happen, but I recommend you don't use JavaScript to do that and let HTML do it's job with the
placeholder
attribute: