Giter Club home page Giter Club logo

searchbar's Introduction

searchBar

模仿百度搜索框

小组件通过jsonp获取百度数据动态添加到虚拟DOM中 效果:

文档结构

结构很简单:

<div class="div">
    <form class="form" autocomplete="off" onsubmit="return sub(this)">
        <input type="text" name="t" class="text"/>
        <input type="submit" value="百度一下" class="submit"/>
        <ul class="ul"></ul>
    </form>
</div>

主要function

//绑定input框输入事件
function onInput(e) {
    var v = e.target.value.trim()
    if (v !== '') {
        //动态jsonp获取数据
        var a = document.createElement('script');
        a.src = 'http://www.baidu.com/su?&wd=' + encodeURI(v) + '&p=3&cb=fcb';
        document.body.appendChild(a);
    }
}
//回调函数
function fcb(data) {
    var newUl = document.createElement("ul");
    var oddUl = document.getElementsByTagName("ul")[0];
    if (data) {
        data.s.forEach(function (val) {
            var li = document.createElement("li");
            li.textContent = val;
            newUl.appendChild(li);
        })
        newUl.className = 'ul';
        newUl.style.display = 'block';
        EventUtil.addHandle(newUl, 'click', function (event) {
            var e = event || window.event;
            var target = e.target || e.srcElement;
            if (target.tagName.toLowerCase() == 'li') {
                var text = target.innerHTML || target.innerText;
                window.open('https://www.baidu.com/s?word=' + text);
                document.getElementsByClassName('text')[0].value = '';
                newUl.style.display = 'none';
            }
        })
        document.getElementsByClassName("form")[0].replaceChild(newUl, oddUl);
        oddUl = null;//gc垃圾回收
    }
    //删除script标签
    var s = document.body.querySelectorAll('script');
    for (var i = 1, len = s.length; i < len; i++) {
        document.body.removeChild(s[i]);
    }
}

searchbar's People

Contributors

zakzhengzq avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.