我有以下代碼:的JavaScript阻止默認KEYUP
$(document).on('keyup', 'p[contenteditable="true"]', function(e) {
if(e.which == 13) {
e.preventDefault();
$(this).after('<p contenteditable = "true"></p>');
$(this).next('p').focus();
} else if((e.which == 8 || e.which == 46) && $(this).text() == "") {
e.preventDefault();
alert("Should remove element.");
$(this).remove();
$(this).previous('p').focus();
};
});
我想阻止默認動作時,按下一個鍵。 preventDefault
適用於keypress
,但不適用於keyup
。有沒有辦法阻止$(document).on('keyup')
的違規行爲?
這是不是爲了問的問題,否則我會給予好評它正確的答案;不過,我真的很感謝你提供這些有用的信息。 –
不使用'keyup',但使用'keydown'並添加他的答案 –