请参阅此http://jsfiddle.net/24g3b2c1/
选择文字,然后点击' PRINT'。然后按'输入'键。 两个事件被解雇了keydown和keypress。
现在评论window.print,只触发一个事件' keydown'
这是Chrome中的预期行为吗?
<body>
<div id="print_key" style="display:block;-webkit-user-select:none;-moz-user-select: none;user-select:none">
PRINT
</div>
<div id="txbx" contenteditable=true>
<p>
<span>Select this text. Then click on PRINT and press (char / enter) key.</span>
</p>
</div>
</body>
<script>
var printKey = document.getElementById('print_key');
printKey.addEventListener("click", clickHandler, false);
printKey.addEventListener("keydown", keyDownHandler, false);
printKey.addEventListener("keypress", keyPressHandler, false);
printKey.setAttribute('tabIndex', -1);
function clickHandler(event) {
document.getElementById('print_key').focus();
}
function keyDownHandler(event) {
console.log('key down on print key')
event.preventDefault();
event.stopPropagation();
window.print();
}
function keyPressHandler(event) {
console.log('keypress on print key')
event.preventDefault();
event.stopPropagation();
}
</script>
我的观察: -
为什么仅通过调用window.print来表现这种差异?
答案 0 :(得分:0)
移动event.preventDefault();在window.print之后,只会解雇keydown