2012-02-27 31 views

回答

4
//wait for our page to be created by jQuery Mobile 
$(document).delegate('#page-id', 'pageinit', function() { 

    //cache the elements we want to show/hide based on the search input 
    var $filterDivs = $('.filter-div'); 

    //bind an event handler to the search input for when it's value changes 
    $('#search').bind('keyup change', function() { 

     //if the value of the input is nothing, then show all the elements 
     if (this.value == '') { 
      $filterDivs.slideDown(500); 

     //otherwise find only the elements that match what's in the search input 
     } else { 

      //create a regular expression based on the value of the search input 
      var regxp = new RegExp(this.value), 

       //get only the elements that match the search term(s) 
       $show = $filterDivs.filter(function() { 
        return ($(this).attr('data-filter').search(regxp) > -1); 
       }); 

      //hide the elements that do not match 
      $filterDivs.not($show).slideUp(500); 

      //and show the elements that do match 
      $show.slideDown(500); 
     } 
    }); 
});​ 

演示:http://jsfiddle.net/jasper/cZW5r/1/

下面是基本的HTML結構用途:

<div data-role="fieldcontain"> 
     <label for="search">Search Input:</label> 
     <input type="search" name="password" id="search" value="" /> 
    </div> 
    <div class="filter-div" data-filter="one">1</div> 
    <div class="filter-div" data-filter="two">2</div> 
    <div class="filter-div" data-filter="three">3</div> 
    <div class="filter-div" data-filter="four">4</div> 
    <div class="filter-div" data-filter="five">5</div> 

注意data-filter屬性,這些屬性是根據搜索項進行檢查的屬性。

1

該鏈接僅向您顯示如何設置搜索輸入文本框。做實際的搜索是你需要自己編程的東西。沒有真正的JavaScript解決方案來搜索您的網站的每一頁。您可以設置JavaScript解決方案來搜索您網站的單個頁面,但這可能不是您想要的。

你可能想看看一些鏈接,看看搜索解決方案:

How to setup a simple site search for a simple website?

Google Custom Search

+0

感謝您的回覆。 – partialdata 2012-02-27 20:47:39