Home > Web Front-end > JS Tutorial > How to handle the default event of a tag hyperlink in javascript_Basic knowledge

How to handle the default event of a tag hyperlink in javascript_Basic knowledge

WBOY
Release: 2016-05-16 15:52:23
Original
1756 people have browsed it

The example in this article describes how JavaScript handles the default event of a tag hyperlink. Share it with everyone for your reference. The specific analysis is as follows:

Sometimes it is necessary to add a click event to the a tag and handle some transactions before jumping, so some processing needs to be done; usually the front end will give a link to represent For this behavior, some people will write it like link or link , but this is not compatible with all browsers, and some browsers will behave strangely.

Therefore, this problem requires other methods to solve. One is to use jquery to block the default event, just like the example given in JQUERY's official API:

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>event.preventDefault demo</title>
 <script src="jquery-1.10.2.js"></script>
</head>
<body>
<a href="http://jquery.com">default click action is prevented</a>
<div id="log"></div>
<script>
$( "a" ).click(function( event ) {
 event.preventDefault();
 $( "<div>" )
  .append( "default " + event.type + " prevented" )
  .appendTo( "#log" );
});
</script>
</body>
</html>
Copy after login

Another method is to add a javascript method to the hyperlink, and add return to the method

Copy code The code is as follows:

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template