原生js模拟百度搜索

1.页面结构

<div class="wrap">
     <div class="inp"><input type="text"></div>
     <div class="result">
     </div>
</div>

2.样式

* {
            margin: 0;
            padding: 0;
        }

        .wrap {
            width: 500px;
            height: 500px;
            margin: 50px auto;
        }

        .wrap .inp {
            width: 500px;
            height: 50px;
            border: 1px solid #051cee60;
        }

        .wrap input {
            display: block;
            width: 100%;
            height: 100%;
            border: none;
            outline: none;
            text-indent: 10px;
            font-size: 15px;
        }

        .result {
            width: 100%;
            border: 1px solid #eee;
            border-bottom-left-radius: 5px;
            border-bottom-right-radius: 5px;
        }

        .result ul {
            list-style: none;
        }

        .result ul li {
            line-height: 24px;
            cursor: pointer;
            text-indent: 20px;
        }

        .result ul li a {
            color: black;
            font-size: 14px;
            text-decoration: none;
        }

3.js

const wrap = document.querySelector('.wrap');
const inp = document.querySelector('.inp');
const input = wrap.querySelector('input');
const result = document.querySelector('.result');

input.onfocus = function () {
      inp.style.cssText = `border: 1px solid #005caf;`;
      result.style.display = 'block'
}
input.onblur = function () {
      inp.style.cssText = `border: 1px solid #051cee60;`;
      result.style.display = 'none'
}
function baidu(data) {
      console.log(data.s.length);
      let ul = '<ul>';
      for (const value of data.s) {
           ul += `
               <li>
                 <a href="#">${value}</a>
               </li>
                `;
      }
      ul += '</ul>';
      result.innerHTML = ul;

      const lilist = document.querySelectorAll('li');
      for (let i = 0; i < lilist.length; i++) {
      		lilist[i].onmouseover = function () {
            	this.style.cssText = `background-color:#c9c9c9`;
            }
            lilist[i].onmouseout = function () {
                this.style.cssText = `background-color:#fff`;
            }
      }
}

input.oninput = function () {
      let sc = document.createElement('script');
      sc.src = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + input.value + '&cb=baidu'
      document.body.appendChild(sc);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值