zoukankan      html  css  js  c++  java
  • 研究了一下Google Ajax Search API, 给博客做了个样品

    效果比较简陋,没有美工的细胞:(
    很多代码都是直接从Google的sample里抄的,我做的工作只是添加了简单的PopUp机制.
    代码相当简单,就不用注释了.

    全部代码:

    CSS:

        <style type="text/css">
            #searcher { 200px; }
            #searchresultdialog.show { display:block; background-color:white; 400px; border:solid 1px #eee; position:absolute; padding:0; }
            #searchresultdialog.hide { display:none; }
            #searchresult .header { font-size:larger;}
            #hideresult, #moreresults { text-align:right; margin-top:0; padding:5px 20px; background:#eee; }
            #searchresult li { border-bottom:solid 1px #eee; }
        </style>


    JavaScript:


    <!--注意:下面的这个key应该是每个人/网站/虚拟目录都不同的, 需要从Google申请. 申请过程完全免费, 应该是百分百成功率. 申请地址: http://code.google.com/apis/ajaxsearch/signup.html -->
        <script src="http://www.google.com/jsapi?key=ABQIAAAAnbDm87eis9d6TnBZLuDj_BT3fZxwUXmDsFtw544tTyLwi1xKvRQWzjl_UJiFlgsvxapvgIGau2FakA" type="text/javascript"></script>
        <script language="Javascript" type="text/javascript">
        //<![CDATA[

        google.load("search", "1");

            function PopupGoogleSearch()
            {
          var searchControl = new google.search.SearchControl();

                this.searcher= new google.search.WebSearch();
          searchControl.addSearcher(this.searcher);
                this.searcher.setResultSetSize(google.search.Search.SMALL_RESULTSET);
                this.searcher.setSiteRestriction("deerchao.cnblogs.com");
                this.searcher.setNoHtmlGeneration();
                this.searcher.setSearchCompleteCallback(this, PopupGoogleSearch.prototype.searchComplete, [this.searcher]);
         
          this.searchForm = new google.search.SearchForm(true, document.getElementById("searchform"));
          this.searchForm.setOnSubmitCallback(this, PopupGoogleSearch.prototype.onSubmit);
          this.searchForm.setOnClearCallback(this, PopupGoogleSearch.prototype.onClear);
         
          this.resultPanel = document.getElementById("searchresult");
          this.moreResults = document.getElementById("moreresults");
            }
           
         PopupGoogleSearch.prototype.searchComplete = function (searcher)
          {
              this.onClear();
              if (searcher.results && searcher.results.length > 0)
              {
                  var ul = document.createElement("ul");
                  for(var i=0; i<searcher.results.length; i++)
                  {
                      var result = searcher.results [i];
                      var li = document.createElement("li");
                      var a = document.createElement("a");
                      a.innerHTML = "<p class='header'><a href ='" + result.url + "'>" +result.title +"</a></p>";
                      li.appendChild(a);
                      var p = document.createElement("div");
                      p.innerHTML = result.content;
                      li.appendChild(p);
                      ul.appendChild(li);
                  }
                  this.resultPanel.appendChild(ul);
                this.showPanel();
                }
          };
         
          PopupGoogleSearch.prototype.onSubmit = function(form)
          {
              if(form.input.value)
              {
                  this.searcher.execute(form.input.value);
                  this.moreResults.innerHTML = "<a href='http://www.google.com/search?q=" + form.input.value +"'>更多结果</a>";
                    }
              return false;
          };
         
          PopupGoogleSearch.prototype.onClear = function ()
          {
              this.resultPanel.innerHTML = "";
              this.hidePanel();
          };
         
          PopupGoogleSearch.prototype.showPanel = function()
          {
              document.getElementById("searchresultdialog").className = "show";
          }
         
          PopupGoogleSearch.prototype.hidePanel = function()
          {
              document.getElementById("searchresultdialog").className = "hide";
          }

            function createDiv(html, class)
            {
            var el = document.createElement("div");
            if (html) {
              el.innerHTML = html;
            }
            if (class) { el.className = class; }
            return el;
        }

        function OnLoad()
        {
            new PopupGoogleSearch();
        }
        google.setOnLoadCallback(OnLoad);

        //]]>
        </script>


    HTML:

        <form id="searcher">
             <div id="searchform">Loading</div>
            </form>
            <div id="searchresultdialog" class="hide">
                <p id="hideresult"><a href="#" onclick="document.getElementById('searchresultdialog').className = 'hide';">关闭</a></p>
            <div id="searchresult"></div>
            <p id="moreresults"></p>
        </div>

  • 相关阅读:
    MVC对session或cookie保存的值在js中做处理
    JQuery判断是否是移动端
    C# Guid 和 JQuery Guid
    JQuery Cookie操作
    DES置换表加密
    RSA的基础运算
    minikatz免杀之msf加载bin文件
    minikatz免杀之Out-EncryptedScript加密
    vue 多选框
    小程序加载更多,上拉刷新
  • 原文地址:https://www.cnblogs.com/deerchao/p/981273.html
Copyright © 2011-2022 走看看