Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 15:52:231716browse

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 ee7959cc8dd4be16ef633321c03dac32link5db79b134e9f6b82c0b36e0489ee08ed to represent For this behavior, some people will write it like 15ce3e7494a3f16b144b37e8fd65c1dalink5db79b134e9f6b82c0b36e0489ee08ed or 280d6902e63098d8a3494133dffa0b82link5db79b134e9f6b82c0b36e0489ee08ed , 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>

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

Copy code The code is as follows:
20ec6db525a54ea2fee4c88c4d11712flink5db79b134e9f6b82c0b36e0489ee08ed

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

Statement:
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