javascript - 使用 Javascript/Jquery 搜索 google.com

标签 javascript jquery

在 google.com 输入上执行检查元素时,我得到:

<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" type="text" value="" aria-label="Search" aria-haspopup="true" role="combobox" aria-autocomplete="both" dir="ltr" spellcheck="false" style="border: none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) transparent; position: absolute; z-index: 6; left: 0px; outline: none;">

现在如果我这样做:$("#lst-ib") ,我按预期得到输入对象。但如果我这样做:$("#lst-ib").val("hello world!") ,我收到错误:

Uncaught TypeError: $(...).val is not a function(…)

Bing.com 也发生了同样的事情。有人可以解释为什么会发生这种情况以及我如何使用 Javascript 在这些网站上进行搜索吗?或Jquery .

最佳答案

原因是 google 不使用 jQuery,所以你不能在那里使用 jQuery 函数。

试试这个

//change the input
document.getElementById("lst-ib").value = "What you want to find";
//submit form
document.getElementById("tsf").submit();

它将按照您的预期更改搜索输入。

关于javascript - 使用 Javascript/Jquery 搜索 google.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40779816/

相关文章:

javascript - 在同一页面上按需提供 HTML 片段

javascript - JShint Ant 任务

javascript - 单击按钮时数组值未正确清空

javascript - 对于每个不按正确顺序发射的情况

javascript - 获取动态创建的行的 id

javascript - vb.net - 执行 selenium ChromeDriver javascript

Javascript:在 document.onload 之前访问 DOM

javascript - React-router:IndexRoute 与 DefaultRoute

jquery - Css3 动画播放两次而不是一次

javascript - 如何删除动态生成的 DIV 的 lastchild 元素并将 html 作为字符串返回