【问题标题】:Is it possible to simulate a Ctrl+F key combination using Jquery?是否可以使用 Jquery 模拟 Ctrl+F 组合键?
【发布时间】:2011-09-07 18:29:43
【问题描述】:

我有一个包含大量信息的页面,如果用户单击一个链接并且浏览器搜索栏弹出,就像他们按下 Ctrl+ 时一样,那就太好了F。我可以查询数据库,因为信息来自那里,但我不想在点击链接时重新加载页面。

【问题讨论】:

标签: javascript jquery browser keyboard-shortcuts


【解决方案1】:

部分浏览器支持window.find()

【讨论】:

  • 这似乎是要走的路。我刚刚在 FF、Chrome 和 IE9 中测试过。
  • @epascarello 假设我要查找的文本出现在多个位置。有什么方法可以计算吗?
【解决方案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
            $('tr').removeClass('item_found');
            //remove item_found class 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")
            }
        }
    });

【讨论】:

  • 我发现了 2 个我建议编辑的错误。此外,我对您的代码进行了一些改进:jsfiddle.net/1shqekzt:利用 CSS,不允许空搜索,并使搜索不区分大小写。进一步改进的想法:添加一个可以在 Enter 键之外使用的提交按钮。
【解决方案3】:

有一些插件可以让你做到这一点,例如:https://github.com/jeresig/jquery.hotkeys

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2019-10-18
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多