2011-09-07 6 views
6

Tôi có một trang có rất nhiều thông tin trong đó và sẽ rất tuyệt nếu người dùng nhấp vào liên kết và thanh tìm kiếm của trình duyệt xuất hiện như khi họ nhấn Ctrl + F. Tôi có thể truy vấn cơ sở dữ liệu, kể từ khi thông tin cames từ đó, nhưng tôi không muốn tải lại trang trên nhấp vào liên kết.Có thể mô phỏng tổ hợp phím Ctrl + F bằng Jquery không?

+2

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

+0

bản sao có thể có của [Sử dụng tìm kiếm trình duyệt (Ctrl + F) thông qua một nút trong trang web?] (htt p: //stackoverflow.com/questions/8080217/use-browser-search-ctrlf-through-a-button-in-website) – Sheepy

Trả lời

6

Một số trình duyệt hỗ trợ window.find()

+1

Điều đó có vẻ giống như cách để đi. Tôi vừa thử nghiệm nó trong FF, Chrome và IE9. –

2

Tôi biết bài này là cũ nhưng tôi nghĩ rằng tôi đã giải quyết này sử dụng 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") 
      } 
     } 
    });