Default Value in the Search Box of the Google Custom Search

微笑、不失礼 提交于 2019-12-24 08:05:10

问题


I am using the standard copy and paste code for the Google Custom Search (free with ads). I want to have some text in the search field when the page is loaded is that possible?

If so, how?

Here's the code:

<div id="cse" style="width: 100%;">Loading</div>
<script src="//www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
  google.load('search', '1', {language : 'en'});
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('000203232594935527974:aspio6dmwkq');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    customSearchControl.draw('cse');
  }, true);
</script>
<link rel="stylesheet" href="//www.google.com/cse/style/look/default.css" type="text/css" />

The floating default Google logo and words "Custom Search" don't bother me.

I was also wondering if I could run the custom search on page load as well.


回答1:


Use the "results only" code. By doing so, you can use your own search box.

If you want some text to show up in the search field, try this:

<form id="cse-search-box" action="http://www.yoursite.com/" method="get">

<input type="text" name="q" autocomplete="off" size="30" onfocus="if(this.value==this.defaultValue)value=''" onblur="if(this.value=='')value=this.defaultValue;" value="THE TEXT YOU WANT HERE" /> 

</form>


来源:https://stackoverflow.com/questions/7857154/default-value-in-the-search-box-of-the-google-custom-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!