이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 참여해서 번역을 마치도록 도와 주세요!
English (US)의 문서도 읽어보세요.

The defaultPrevented read-only property of the Event interface returns a Boolean indicating whether or not Event.preventDefault() was called on the event.

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

Syntax

var defaultWasPrevented = event.defaultPrevented;

Value

A Boolean, where true indicates that the default user agent action was prevented, and false indicates that it was not.

Example

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);

Result

Specifications

Specification Status Comment
DOM
The definition of 'Event.defaultPrevented()' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 18Edge Full support YesFirefox Full support 6IE Full support 9Opera Full support 11Safari Full support 5WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 6Opera Android Full support 11Safari iOS Full support 5Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

문서 태그 및 공헌자

최종 변경자: mfluehr,