Google搜索在HTML和JavaScript中不起作用。

huangapple go评论72阅读模式
英文:

Google search not working by html and javascript

问题

以下是您要翻译的部分:

"Html -

<div class="search-container">
    <form>
        <img src="imgs and icons/google.png" alt="" class="g-icon">
        <input class="search" type="text" name="myInput"/>
        <button id="search-button" onclick="googleSearch()"><img src="imgs and icons/search.png" class="search-icon" alt=""></button>
    </form>
</div>

and here is javascript-

function googleSearch()
{
    var text=document.getElementsByClassName("search").value;
    var cleanQuery = text.replace(" ","+",text);
    var url='http://www.google.com/search?q='+cleanQuery;

    window.location.href=url;
}

Please tell me what mistake is there in this code and why is it not working"

英文:

So i was working on a project where i needed to make a search box by which when a button is clicked it searches the value in the input in new tab on google so i wrote some code to make it possible.

In order to make the google search by my page i wrote the following code
Html -

<div class="search-container">
    <form>
        <img src="imgs and icons/google.png" alt="" class="g-icon">
        <input class="search" type="text" name="myInput"/>
        <button id="search-button" onclick="googleSearch()"><img src="imgs and icons/search.png" class="search-icon" alt=""></button>
    </form>
</div>

and here is javascript-

function googleSearch()
{
    var text=document.getElementsByClassName("search").value;
    var cleanQuery = text.replace(" ","+",text);
    var url='http://www.google.com/search?q='+cleanQuery;

    window.location.href=url;
}

Please tell me what mistake is there in this code and why is it not working

答案1

得分: 1

  1. document.getElementsByClassName 返回的是 NodeList

更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

  1. String.prototype.replace() 只接受两个参数。

更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

由于第1点,您的代码无法正常工作。

英文:
  1. document.getElementsByClassName return is NodeList

More info: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

  1. String.prototype.replace() only accept 2 parameters

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

> Your code is not working due to thing number 1

function googleSearch()
    {
        var text=document.getElementsByClassName("search")[0].value;
        var cleanQuery = text.replace(" ","+");
        var url='http://www.google.com/search?q='+cleanQuery;
    
        window.location.href=url;
    }

答案2

得分: 0

下午好!
首先,您应该查看一下您的 var text 实际上存储了什么。getElementsByClassname 返回一个元素数组(HTMLCollection)。

您可以像这样索引它的第一个元素

var text=document.getElementsByClassName("search")[0].value;

// 或者使用 querySelector()

var text = document.querySelector('.search').value // 只返回第一个匹配项,要查看数组结果,请参见 querySelectorAll()

注意:querySelector('[css like string]') 使用与您的 CSS 选择器相同的语法,因此 .name 将查找具有 class 为 name 的元素,#name 将查找具有 id 为 name 的元素。

无论哪种方式,希望其中任何一个对您有帮助!


编辑;如果您只打算在 Google 中使用它,那么替换空格字符是多余的,?q=test value?q=test+value?q=test%20value 的结果相同。


编辑 2
为了在新标签页/窗口中打开 URL,您不能使用 window.location.href,在这种情况下,window 指的是您当前的窗口实例,这就是为什么他们提供了 window.open(url, target)

target 用于指示窗口应该在哪里打开,通常是 _blank,请参阅 target 关键字 以获取更详细的解释。

英文:

Good afternoon!

So the first thing you should take a look at is what your var text is actually storing. getElementsByClassname returns an array of elements (HTMLCollection).

You can either index it for it's first element like so

var text=document.getElementsByClassName("search")[0].value;

// or use querySelector()

var text = document.querySelector('.search').value // only returns the first match, for an array result see querySelectorAll()

Note: querySelector('[css like string]') uses the same syntax as your css selectors so .name will look for elements with class of name and #name will look for elements with the id of name

Either way hope any of these help you out!


Edit; the replacements of whitespace characters is redundant if you only intend to use it with google, ?q=test value yields the same results as ?q=test+value or ?q=test%20value


Edit 2;
In order to open the url in a new tab/window you cannot use window.location.href, window in this case refers to your current window instance, that's why they've provided us with window.open(url, target)

target is used to indicate where the window should open to, usually _blank, see target keywords for a more detailed explanation

huangapple
  • 本文由 发表于 2023年3月9日 18:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定