2011-09-07 115 views
6

我有很多在這信息的頁面,如果用戶點擊了一個鏈接和瀏覽器的搜索欄上彈出,因爲它會如果他們按下Ctrl鍵+˚F這將是很好。我可以查詢數據庫,因爲信息來自那裏,但我不想重新加載鏈接點擊頁面。是否有可能使用Jquery模擬Ctrl + F組合鍵?

+2

http://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery – Neal

+0

[通過網站上的按鈕使用瀏覽器搜索(Ctrl + F)?]可能的重複(HTT p://stackoverflow.com/questions/8080217/use-browser-search-ctrlf-through-a-button-in-website) – Sheepy

回答

6

一些瀏覽器支持window.find()

+1

這似乎是要走的路。我只是在FF,Chrome和IE9中測試它。 –

2

我知道這個職位是舊的,但我想我解決了這個使用jquery:

//i am assuming you are searching through a table... 
    //search input field listening for key pressed 
    $('.search_input').keyup(function (e) { 
     //listening if the key pressed is the enter button 
     if (e.which === 13) { 
      //inserting the value of textfield content, you can add if statement to check if the field is null or empty 
      var search_param = $(this).val(); 
      //value of field stored into a variable 
      $('td').removeAttr('class'); 
      //remove all classes attributed to a td AND search all td to find the one that march the search parameter 
      if ($('td:contains("' + search_param + '")').html() !== undefined) { 
       //if there is any td that has that record... then check for the closest tr and add a class with item_found as value 
       $('td:contains("' + search_param + '")').closest('tr').attr('class', 'item_found'); 
       //add some highlight to it. 
       $('td:contains("' + search_param + '")').closest('tr').css({background:'yellow',color:black}); 
       //then scroll to the item 
       $(window).scrollTop($('.item_found').first().offset().top); 
      } else { 
       //if item is not found display no result found 
       alert("Sorry no result found") 
      } 
     } 
    }); 
相關問題