简体   繁体   English

如何使用 css、js 和 html 将谷歌搜索面板添加到我的网站?

[英]How would i add the google search panel to my website with css, js and html?

Im currently working on my project NightScape which is a landing page for myself and others and i wanted to add a google search bar but all ive managed to do is make a text box which you can write in. Here is my original code, any ideas?我目前正在处理我的项目 NightScape,这是我和其他人的登录页面,我想添加一个谷歌搜索栏,但我所做的只是制作一个可以写入的文本框。这是我的原始代码,任何想法?

HTML HTML

<div class="nightsearch">
  <header>
    <input type="text" autocomplete="off" class="nightsearchbox" placeholder="Search Google...">
  </header>
</div>

JS JS

function search() {
var nightsearchbox = document.getElementById("nightsearchbox").value;
location.replace("https://www.google.com/search?q=" + nightsearchbox + "");
}

CSS CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.nightsearch {
  width: 100%;
  max-width: 680px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 10vh;
  background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05));
  position: absolute;
  bottom: 60px;
}

.nightsearchbox {
  height: 100%;
  max-height: 30px;
  width: 400%;
  max-width: 660px;
  display: flex;
  flex-direction: left;
  align-items: left;
  justify-content: left;
  text-align: left;
  min-height: 7vh;
  position: absolute;
  top: 15px;
  left: 10px;
}

在此处输入图像描述

Something like this may be you want.像这样的东西可能是你想要的。 Just add a search button as well to make sure user clicks on it after entering the search words and then use location.replace to open up the google search page with you query words with an onclick function.只需添加一个search按钮,以确保用户在entering搜索词后clicks它,然后使用location.replace打开谷歌搜索页面,您使用onclick function 查询词。

Also you do not have an id => .nightsearchbox its a class of your input.此外,您没有id => .nightsearchbox ,它是您输入的class We can access this class using querySelector method and then its value to pass it as query to google search page.我们可以使用querySelector方法访问这个 class,然后将其值作为查询传递给google搜索页面。

In addtion to make sure that search icon (button) stays next to the actual search input we need to wrap button input and button that in div为了确保搜索icon (按钮)位于实际搜索输入旁边,我们需要将按钮inputbutton包装在div

Live Working Demo:现场工作演示:

 function search() { var nightsearchbox = document.querySelector(".nightsearchbox").value; location.replace("https://www.google.com/search?q=" + nightsearchbox + ""); }
 .nightsearch { width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; min-height: 10vh; background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05)); position: absolute; bottom: 60px; }.nightsearchbox { height: 100%; max-height: 30px; max-width: 660px; text-align: left; min-height: 7vh; }.google_search { display: flex; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <div class="nightsearch"> <header> <div class="google_search"> <input type="text" autocomplete="off" class="nightsearchbox" placeholder="Search Google..."> <button onclick="search()"> <i class="fa fa-search" aria-hidden="true"></i> </button> </div> </header> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM