gpt4 book ai didi

javascript - 在 javascript 中调用自定义谷歌搜索标签时出现问题

转载 作者:行者123 更新时间:2023-11-27 23:28:43 26 4
gpt4 key购买 nike

我正在使用 google 自定义搜索 api,但我不想要 google 搜索默认文本框,因此出于同样的原因,我创建了一个新的文本框和一个按钮。单击我的自定义按钮后,我将从我的自定义文本框中获取搜索键值并将其放入谷歌搜索默认文本框中。但是在我的javascript函数中我无法执行谷歌搜索提交按钮的点击事件。我现在知道谷歌搜索提交按钮的类名,我如何在我的 java 脚本函数中执行点击事件?

google 搜索 dubmit 按钮类名称是“gsc-search-button gsc-search-button-v2”

现在在下面的方法中我需要执行上面类的点击事件。

<script>
function myFunction() {
document.getElementById('gsc-i-id1').value = document.getElementById('customTextBox').value;
// here I need to perform click event for "gsc-search-button gsc-search-button-v2" class
}
</script>script>

我在下面的链接中有演示示例,

http://jsfiddle.net/3L4fd63g/3/

有什么建议吗

最佳答案

我看到 标记,所以这是一个用于提交表单的选项,因此文本框上的回车键也将执行搜索。

此外,要执行点击,您只需调用 .click()

// Make sure in jsFiddle you have selected option onLoad.
(function() {
var cx = '017643444788069204610:4gvhea_mvga'; // Insert your own Custom Search Engine ID here
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);
})();

$(function(){

$('#search').submit(function(e){
$('.gsc-input').val($('#customTextBox').val());
$('input.gsc-search-button').click();
e.preventDefault();
});

});
.search-box{
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="search">
<input type="text" id="customTextBox"/>
<input type="submit" value="Click me" />
</form>

<div class="search-box">
<gcse:searchbox></gcse:searchbox>
</div>

<gcse:searchresults></gcse:searchresults>

关于javascript - 在 javascript 中调用自定义谷歌搜索标签时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com