【问题标题】:jQuery - even.keyCode not being recognized correctly in firefox?jQuery - even.keyCode 在 Firefox 中没有被正确识别?
【发布时间】:2013-03-05 18:27:56
【问题描述】:

以下代码在 IE 和 Chrome(最新版本)中正常工作

$('#searchBox').keyup(function () {
    var searchTxt = $(this).val();

    // if item contains searchTxt,
    if ($('.item:contains("' + searchTxt + '")')) {
        // hide the items that do NOT contain searchTxt
        $('.item').not(':contains(' + searchTxt + ')').hide();
    };

    // capture backspace
    if (event.keyCode == 8) {
        // show item that contains searchTxt
        $('.item:contains(' + searchTxt + ')').show();
    };

    // if search box is empty, 
    if ($('#searchBox').val() == "") {
        // show all items
        $('.item').show();
    };
});

上面的代码执行“区分大小写的实时搜索”,并且无法执行在 Firefox 中捕获退格键的代码块:

// capture backspace
if (event.keyCode == 8) {
    // show item that contains searchTxt
    $('.item:contains(' + searchTxt + ')').show();
};

【问题讨论】:

    标签: jquery events firefox live keycode


    【解决方案1】:

    event 参数放入:

    $('#searchBox').keyup(function (event) { .. });

    而不是keyCode 用户event.which

    阅读更多关于 event.which

    【讨论】:

      【解决方案2】:

      你需要声明事件对象。

      $('#searchBox').keyup(function (event) {
      

      【讨论】:

        【解决方案3】:

        像这样在函数中传递事件:

        $('#searchBox').keyup(function (event) {
        
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-09
          • 1970-01-01
          • 1970-01-01
          • 2021-11-14
          • 2020-11-05
          • 1970-01-01
          • 2021-06-12
          • 1970-01-01
          相关资源
          最近更新 更多