简体   繁体   English

无法实现js功能的跨浏览器兼容性

[英]Cannot achieve cross browser compatibility for js function

The code below works for disabling the backspace key in a textarea in Firefox perfectly but not Chrome or Safari, any suggestions would be very much appreciated 下面的代码用于在Firefox中完美禁用文本区域中的退格键,但不能禁用Chrome或Safari,任何建议将不胜感激

$('#texttype').keypress(function(event){ 
var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '8'){ 
        return false;
    }
        event.stopPropagation();

}); });

Why not use e.which , it's normalized in jQuery, and the keycode is an integer. 为什么不使用e.which ,它在jQuery中已标准化,并且键码是整数。

The keydown event triggers on any key press and gives a keycode in all browsers. keydown事件在任何按键按下时触发,并在所有浏览器中提供一个键码。

The keypress event triggers after keydown and does give a keycode, but it's only guaranteed for character keys, and does not fire on the backspace key in webkit. keypress后,事件触发keydown ,并确实给了邀请码,但它只能保证字符键,而不会触发在WebKit的退格键。

$('#texttype').on('keydown', function(e) { 
    if ( e.which === 8 ) { 
        return false;
    }
    e.stopPropagation();
});

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM