有没有办法检测到较低的(DOM 0)事件侦听器阻止了默认的浏览器操作?
似乎IE8及以下版本不会将“return false”传播到“event.returnValue”中。以下适用于所有其他浏览器。
...
<script>
function addListener(obj, type, listener) {
var w3c = 'addEventListener', ie = 'attachEvent';
if (obj[w3c]) obj[w3c](type, listener);
else if (obj[ie]) obj[ie]('on' + type, listener);
};
addListener(document, 'click', function(event) {
event = event || window.event;
if (event.defaultPrevented || event.returnValue === false ||
event.getPreventDefault && event.getPreventDefault()) {
return;
}
alert('Default action not prevented!');
});
</script>
...
<!-- THIS PART CANNOT BE CHANGED. -->
<a href="//foo.com" onclick="return confirm('Really leave page?');">click me</a>