【问题标题】:Browser compatibility with Firefox and IE keycode浏览器与 Firefox 和 IE 键码的兼容性
【发布时间】:2014-10-19 18:39:53
【问题描述】:

我得到了这个用于自动建议搜索的 Jquery 代码,向上和向下箭头的键码非常适合 chrome 和 safari,但是当我单击 firefox 或 IE 上的箭头时没有响应。我环顾四周,尝试使用 charCode,甚至为“keyCode”添加了一个变量,即 e.which 或 e.keyCode。仍然没有任何反应。

(function($){
$.fn.fsearch = function(){
var $searchInput = $(this);
$searchInput.after('<div id="divResult"></div>');
$resultDiv = $('#divResult');
$searchInput.focus();
$searchInput.addClass('searchi');
$resultDiv.html("<ul></ul>");
$searchInput.keyup(function(e) {
var q=$(this).val();
var keyCode = (window.event) ? e.which : e.keyCode;
if(q != '')
{ 
  var current_index = $('.selected').index(),
  $options = $resultDiv.find('.option'),
  items_total = $options.length;

  // When Down Arrow key is pressed
  if (keyCode == 40) {
      if (current_index + 1 < items_total) {
          current_index++;
          change_selection($options,current_index);
      }
  }


  // When Up Arrow is pressed
  else if (keyCode == 38) {
      if (current_index > 0) {
          current_index--;
          change_selection($options,current_index);
      }
  }

  // When enter key is pressed
  else if(keyCode == 13){
    var id = $resultDiv.find('ul li.selected').attr('id');
    var name = $resultDiv.find('ul li.selected').find('.name').text();
    window.location = "event.listing.php?id="+id;
  }
  else{
  $resultDiv.fadeIn();

  // Query search details from database
  $.getJSON("json.php",{searchword: q},function(jsonResult)
  { 
    var str='';
    for(var i=0; i<jsonResult.length;i++)
      {
        str += '<li id=' + jsonResult[i].id + ' class="option"><img class="profile_image" src="data:image/jpg;base64,'+jsonResult[i].photo+'" alt="'+jsonResult[i].fullName+'"/><span class="name">' + jsonResult[i].fullName + '</span><br /><span class="name" style="font-size:10px; color:#999;">'+jsonResult[i].tel+'</span></li>';
      }
      $resultDiv.find('ul').empty().prepend(str);
      $resultDiv.find('ul li').first().addClass('selected');
  }); 

    $resultDiv.find('ul li').live('mouseover',function(e){
    current_index = $resultDiv.find('ul li').index(this);
    $options = $resultDiv.find('.option');
    change_selection($options,current_index);
  });

  // Change selected element style by adding a css class
  function change_selection($options,current_index){
    $options.removeClass('selected');
    $options.eq(current_index).addClass('selected');
    }
  }
}
    else{
  //Hide the results if there is no search input
  $resultDiv.hide();
   }
   });    

   // Hide the search result when clicked outside
     jQuery(document).live("click", function(e) { 
     var $clicked = $(e.target);
     if ($clicked.hasClass("searchi") || $clicked.hasClass("searchf")){
     }
     else{
     $resultDiv.fadeOut(); 
     }
     });

     // Show previous search results when the search box is clicked
     $searchInput.click(function(){
       var q=$(this).val();
       if(q != '')
      { 
       $resultDiv.fadeIn();
       }
       });

       // Select the element when it is clicked
        $resultDiv.find('li').live("click",function(e){ 
        var id = $(this).attr('id');
        var name = ($(this).find('.name').text());
        window.location = "event.listing.php?id="+id;
        });

        };
      })(jQuery);

【问题讨论】:

    标签: jquery internet-explorer firefox cross-browser keycode


    【解决方案1】:

    你已经标记了 jQuery,所以你应该只使用 event.which:

    event.which 属性规范化了event.keyCodeevent.charCode。键盘按键输入建议观看event.which

    var keyCode = e.which;
    

    【讨论】:

    • 我以前用过,在 Firefox 或 IE 中仍然没有任何反应。如果这有帮助,我正在使用 Firefox 31.0,但无论浏览器构建如何,我都希望它兼容。
    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2011-11-05
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多