2016-01-21 51 views
0
// keyboard down window check. 
window.addEventListener("keydown", function(event) { 
    onKeyDownHandler(event); 
}, false); 

function onKeyDownHandler(e) 
{ 
e.preventDefault(); 
    var focus_id = e.target.id; 
    switch (e.keyCode) { 
     case 46: // delete 
     //delete item 
     alert("why i cant get this work on mac?"); 
     break; 
} 
} 

爲什麼我的按鍵不能在mac上工作?任何人都可以告訴我如何使按鍵刪除工作在Mac?如何在macbook上捕獲刪除按鍵

回答

0

Backspace(8)和delete(46)在Mac上執行相同的操作。

更新代碼:

// keyboard down window check. 
window.addEventListener("keydown", function(event) { 
    onKeyDownHandler(event); 
}, false); 

function onKeyDownHandler(e) 
{ 
    e.preventDefault(); 
    var focus_id = e.target.id; 
    switch (e.keyCode) { 
    case 46: // Delete 
    case 8: // Backspace 
     alert("Delete pressed"); 
     break; 
    } 
} 
0
var input = document.getElementById('myInput'); 

input.onkeydown = function() { 
    var key = event.keyCode || event.charCode; 

    if(key == 8 || key == 46) 
     return false; 
}; 

=>按有關Jquery的標記 -

jQuery(function($) { 
     var input = $('#myInput'); 
     input.on('keydown', function() { 
     var key = event.keyCode || event.charCode; 

     if(key == 8 || key == 46) 
      return false; 
     }); 
});