Prevent a link from opening the URL, and check if preventDefault()
was called:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from w w w . j a va 2s. c o m <script> $(document).ready(function(){ $("a").click(function(event){ event.preventDefault(); document.getElementById("demo").innerHTML = "Was preventDefault() called: " + event.isDefaultPrevented(); }); }); </script> </head> <body> <p id="demo"></p> <a href="http://java2s.com/">Go to java2s.com</a> <p>Click the link and check if the default action is prevented.</p> </body> </html>
The event.isDefaultPrevented()
method checks whether the preventDefault()
method was called for the event.
event.isDefaultPrevented()
Parameter | Optional | Description |
---|---|---|
event | Required. | The event parameter comes from the event binding function |