前端js实现搜索功能(新手向-转到必应搜索,可在js代码第三行更改引擎)

先看一下基于此做的小案例,核心就是那三行js,原理很简单,但是这个思路很棒
在这里插入图片描述

话不多说,上代码(粘贴可以直接使用),js部分href可以更改链接来切换不同的搜索引擎

html
        <form onsubmit="return search()">
            <input type="text" class="search" placeholder="想搜点啥" autocomplete="off" autofocus>
            <button onclick="return search">搜索</button>
        </form>
js部分
        function search() {
            if (document.querySelector(".search").value != "") {
                window.location.href = "https://cn.bing.com/search?q=" + document.querySelector(".search").value;
                document.querySelector(".search").value = "";
            }
            return false;
        }

上面动图代码如下,核心就是上面的两端代码,只是加了一点样式而已,很简单

<!DOCTYPE html>
<html lang="zh">
    <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">
        <title>搜索框</title>
    </head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .search-box {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            border-radius: 25px;
            height: 40px;
            padding: 10px;
            background-color: #CCC;
        }
        .search-box:hover>.search {
            width: 200px;
            padding: 0 6px;
        }
        .search-box:hover>.search-cli {
            transform: rotateX(360deg);
            transition: 0.8s;
        }
        .search-cli {
            float: right;
            width: 40px;
            height: 40px;
            border: none;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: 0.4s;
            background-color: #CCC;
        }
        .search {
            border: none;
            background: none;
            outline: none;
            float: left;
            padding:0;
            color: white;
            font-size: 16px;
            line-height: 40px;
            transition: 0.4s;
            width: 0;

        }
        img {
            width: 100%;
            height: auto;
        }
    </style>
    <body>
        <form class="search-box" onsubmit="return search()">
            <input type="text" class="search" placeholder="想搜点啥" autocomplete="off" autofocus>
            <button class="search-cli" onclick="return search">
                <img src="./img/放大镜.svg" alt="">
            </button>
        </form>
    </body>

    <script>
        function search() {
            if (document.querySelector(".search").value != "") {
                window.location.href = "https://cn.bing.com/search?q=" + document.querySelector(".search").value;
                document.querySelector(".search").value = "";
            }
            return false;
        }
    </script>
</html>
  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值