Leo*_*Leo 8 html javascript google-custom-search
所以在我的网站上我使用谷歌自定义搜索引擎,当我搜索示例'youtub'而不是'youtube'it显示为一个建议(显示结果为youtube - 搜索而不是youtub)或(你的意思是:youtube)
所以当我点击文本youtube时,我想将它添加到我的搜索框中,我该怎么做?这是我的代码
searchbox .html
<form action="" method="GET" id="searchform">
<input required="" type="search" name="q" id="searchbox" placeholder="Search..." title="Search..." enableAutoComplete="true" autofocus/>
<button><div class="Search-Button"><img src="MYIMGHERE" class="search-png"/></div></button>
</form>
Run Code Online (Sandbox Code Playgroud)
.js文件
<script type="text/javascript">
(function() {
var cx = '007072537540236505002:fsvzbpwgtgc';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
var urlParams = new URLSearchParams(window.location.search);
document.getElementById('searchbox').value = urlParams.get('q');
</script>
<gcse:searchresults-only linktarget="_self"></gcse:searchresults-only>
Run Code Online (Sandbox Code Playgroud)
我的问题不是很明确,因为我找不到合适的词语!我很抱歉.
解决方案!
$(document).on('click', '.gs-spelling', function(e) {
window.location.search = 'q=' + ($(this).find('i').text());
document.getElementById('searchbox').value = ($(this).find('i').text());
});
Run Code Online (Sandbox Code Playgroud)