defaultPrevented 只读特性在 事件 interface returns a 布尔 indicating whether or not the call to Event.preventDefault() canceled the event.

注意: You should use this instead of the non-standard, deprecated getPreventDefault() method (see bug 691151 ).

句法

var defaultWasPrevented = event.defaultPrevented;
					

A 布尔 ,其中 true indicates that the default 用户代理 action was prevented, and false indicates that it was not.

范例

This example logs attempts to visit links from two <a> elements. JavaScript is used to prevent the second link from working.

HTML

<p><a id="link1" href="#link1">Visit link 1</a></p>
<p><a id="link2" href="#link2">Try to visit link 2</a> (you can't)</p>
<p id="log"></p>
					

JavaScript

function stopLink(event) {
  event.preventDefault();
}
function logClick(event) {
  const log = document.getElementById('log');
  if (event.target.tagName === 'A') {
    if (event.defaultPrevented) {
      log.innerText = 'Sorry, but you cannot visit this link!\n' + log.innerText;
    }
    else {
      log.innerText = 'Visiting link...\n' + log.innerText;
    }
  }
}
const a = document.getElementById('link2');
a.addEventListener('click', stopLink);
document.addEventListener('click', logClick);
					

结果

规范

规范 状态 注释
DOM
The definition of 'Event.defaultPrevented()' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
defaultPrevented Chrome 18 Edge 12 Firefox 6 IE 9 Opera 11 Safari 5 WebView Android Yes Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 5 Samsung Internet Android 1.0

图例

完整支持

完整支持

元数据

  • 最后修改: