在 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("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") 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/