Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Event: defaultPrevented property

BaselineWidely available

Note: This feature is available inWeb Workers.

ThedefaultPrevented read-only property of theEvent interface returns a boolean value indicating whether or not the call toEvent.preventDefault() canceled the event.

Value

A boolean value, wheretrue indicates that the defaultuser agent action was prevented, andfalse 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

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

JavaScript

js
function stopLink(event) {  event.preventDefault();}function logClick(event) {  const log = document.getElementById("log");  if (event.target.tagName === "A") {    log.innerText = event.defaultPrevented      ? `Sorry, but you cannot visit this link!\n${log.innerText}`      : `Visiting link…\n${log.innerText}`;  }}const a = document.getElementById("link2");a.addEventListener("click", stopLink);document.addEventListener("click", logClick);

Result

Specifications

Specification
DOM
# ref-for-dom-event-defaultprevented①

Browser compatibility

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp