在API和Display API中搜索

时间:2017-04-21 12:35:20

标签: json search

如何搜索任何关键字

我有两个不同的代码块,但我不知道如何将它组合起来

我想要的是当你在第二个盒子里搜索它时会显示第一个编码。如果没有按钮,我不介意。

 <html>
      <head>
        <title>JSON/Atom Custom Search API Example</title>
      </head>
      <body>
        <div id="content">
            <input type="text" id="Query">
            <button id="add">Search</button>
        </div>

        <script>
         function hndlr(response) {
        for (var i = 0; i < response.items.length; i++) {
            var item = response.items[i];

            // in production code, item.htmlTitle should have the HTML entities escaped.
            document.getElementById("content").innerHTML +="<br>"+"<b>"+ item.title + "</b>"+"<br>" +item.snippet  + "<br>"+"<a href='"+item.link+"'>"+item.displayLink+"</a>" ;

        }
    }
        </script>

        <script id="fixme"></script>
        <script type="text/javascript">
        var search_query = 'https://www.googleapis.com/customsearch/v1?q=Query&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&callback=hndlr';
        document.getElementById('fixme').src = search_query;
    </script>

    <script>
    function showHint(str) {
        if (str.length == 0) { 
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            }
            xmlhttp.open("GET", "https://www.googleapis.com/customsearch/v1?q="+str+"&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&callback=hndlr", true);
            xmlhttp.send();
        }
    }
    </script>
    </head>
    <body>

    <p><b></b></p>
    <form> 
    <input type="text" onkeyup="showHint(this.value)">
    </form>
    <p>Answer <span id="txtHint"></span></p>
      </body>
    </html>

了解更多详情

http://mediamonitoring.comyr.com/jquery3.php

0 个答案:

没有答案