【问题标题】:add search button on search box on Custom Search Engine在自定义搜索引擎的搜索框中添加搜索按钮
【发布时间】:2018-06-23 14:26:47
【问题描述】:

如何将搜索按钮添加到HTML?我已经尝试添加它,但是当我搜索示例YouTube 时显示结果但不显示我的搜索文本YouTube 我该如何解决这个问题,我需要在此代码中更改什么?谢谢

.main{
	position: absolute;
	top: 10px;
    left: 25%;
	width: 50%;
	height: auto;	
	text-align: center;
}
.q{
	padding: 5px;
	width: 90%;
	margin-top: -30px;
	border: solid 1px #c68e00;
	font-size: 17px;
	font-family: Arial;
	height: 40px;
}
<script>
    (function() {
        var cx = '004533415228465222070:r2ak9s12hwi';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
    })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
</div>

<div class="main">
	<br/><br/>
	<form action="" method="GET">
 <input class="q" name="q" placeholder="Search by Name or type URL" title="Search ..." value="" /> 
</form>

【问题讨论】:

    标签: javascript jquery html search google-search-api


    【解决方案1】:

    我不确定结果,但希望能成功。

    在搜索输入中添加了id="searchbox"。还有这两行。

    var urlParams = new URLSearchParams(window.location.search);
    document.getElementById('searchbox').value = urlParams.get('q');
    

    您的完整代码现在看起来像。

    <div class="main">
        <br/><br/>
        <form action="" method="GET">
     <input class="q" name="q" id="searchbox" placeholder="Search by Name or type URL" title="Search ..." value="" /> 
    </form>
    </div>
    <script>
        (function() {
            var cx = '004533415228465222070:r2ak9s12hwi';
            var gcse = document.createElement('script');
            gcse.type = 'text/javascript';
            gcse.async = true;
            gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/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></gcse:searchresults-only>
    

    【讨论】:

    • 感谢您的代码运行良好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多