您可以尝试阻止对任意数量的事件执行默认操作。请注意,浏览器通常允许这样做,但有最终决定权(我模糊地记得这并不总是有效的一些问题)。
var events = ["keypress","click","submit","focus","blur","tab"];
events.forEach(function(eventName){
window.addEventListener(eventName, function(e){
// this is the list of tags we'd like to prevent events on
// you may wish to remove this early return altogether, as even a div or span
// can cause a form to submit with JavaScript
if (["INPUT","A","FORM","BUTTON"].indexOf(e.tagName) === -1) {
return;
}
// prevent the default action
e.preventDefault();
// stop other JavaScript listeners from firing
e.stopPropagation();
e.stopImediatePropagation();
// if an input is somehow focused, unfocus it
var target = e.target;
setTimeout(function(){
if (target.blur) target.blur();
}, 50);
// true as the third parameter signifies 'use capture' where available;
// this means we get the event as it's going down, before it starts to bubble up
// our propagation prevention will thus also prevent most delegated event handlers
}, true);
});
你不能用 CSS 解决这个问题。 pointer-events
不允许您指定哪些指针事件是可以的。为此,您确实需要 JavaScript。