【问题标题】:How to make a Search Bar that searches on google using html/css/js [closed]如何制作一个使用 html/css/js 在谷歌上搜索的搜索栏 [关闭]
【发布时间】:2022-12-15 07:33:04
【问题描述】:

这是学习 Web 开发基础知识的另一种尝试。

我创建了一个简单的搜索栏。

我想做的是: 如果我们在提供的文本框中输入任何内容,然后我按搜索按钮或回车键, t 在谷歌上搜索提供的文本。

我想知道我们如何使用 JS 编写代码

let textInput=document.querySelector('.textInput');
let searchBtn=document.querySelector('.searchBtn');

searchBtn.addEventListener('click',()=>{
    
});

searchBtn.addEventListener("keydown", (e)=> {
    if (e.key === "Enter") {
       
    }
});
body{
    display: grid;
    place-items:center;
    height: 100vh;
    align-content: center;
    justify-content: center;
}

.textInput input{
    width: 300px;
    height: 30px;
}

h3 {
    margin-bottom: 10px;
}

.searchBar{
    display: flex;
    align-items: center;
}
.searchBtn{
    width: 50px;
    height: 36px;
    background-color: rgb(192, 192, 192);
    display: grid;
    place-items: center;
    cursor: pointer;
}

.searchBtn img{
    width: 30px;
    height: auto;
    cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styleSheet.css">
    <title>Document</title>
</head>
<body>
    <div class="title"><h3> HELLO SEARCH</h3></div>
    <div class="searchBar">
        <div class="textInput">
            <input type="text" placeholder="Search Enything">
        </div>
        <div class="searchBtn"><img src="https://img.icons8.com/ios-glyphs/60/000000/search--v1.png"/></div>
    </div>
    <script src="app..js"></script>
</body>
</html>

谢谢你!

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    你不需要 JavaScript 来让它在谷歌上搜索。您可以只添加一个表单操作,使其进入谷歌。只需将您的 HTML 更改为:

    <div class="title">
      <h3> HELLO SEARCH</h3>
    </div>
    <form action="https://www.google.com/search">
      <div class="searchBar">
        <div class="textInput">
          <input type="text" name="q" placeholder="Search Enything">
        </div>
        <button type="submit" class="searchBtn">
          <img src="https://img.icons8.com/ios-glyphs/60/000000/search--v1.png" />
        </button>
      </div>
    </form>

    【讨论】:

      【解决方案2】:

      尝试使用这个 JavaScript

      let textInput=document.querySelector('.textInput');
      let searchBtn=document.querySelector('.searchBtn');
      
      searchBtn.addEventListener('click',()=>{
          
      });
      
      searchBtn.addEventListener("keydown", (e)=> {
          if (e.key === "Enter") {
            
             var text = document.getElementById("inputid");
             location = `google.com/search?q=${text}&safe=active` 
             
          }
      });
      
      

      并为你的输入字段添加一个id属性,并将上面的“inputid”替换为id的值。

      【讨论】:

      • 你不应该在 JS 中混合使用单引号和双引号。特别是对于 JS 来说,最好只使用单引号,而不是为脚本使用双引号,而是为可能通过 JS 操作的 HTML 使用双引号。然后,作为一个好的做法,我建议您将此行:location = "https://www.google.com/search?+"text+"=hi&amp;safe=active"; 更改为:location = google.com/search?${text}=hi&safe=active``
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2022-12-02
      • 2013-03-09
      相关资源
      最近更新 更多