We often use click events in the a tag: 1. a href="javascript:js_method();" This is a commonly used method on our platform, but this method is prone to problems when passing parameters such as this. In addition, when the javascript: protocol is used as the href attribute of a, it will not only cause unnecessary triggering of the window.onbeforeunload event, but will also stop the gif animation from playing in IE. W3C standards do not recommend executing JavaScript statements in href 2. a href="javascript:void(0);" onclick="js_method()" This method is the most commonly used method for many websites and is also the most comprehensive method. The onclick method is responsible for executing the js function, and void is an operator. void(0) returns undefined and the address does not jump. And this method will not directly expose the js method in the browser status bar like the first method. 3.a href="javascript:;" onclick="js_method()" This method is similar to the previous two methods, the only difference is that an empty js code is executed. 4.a href="#" onclick="js_method()" This method is also a very common code on the Internet. # is a method built into the tag, representing the function of top. So using this method, after clicking on the web page, you will return to the top of the page. 5.a href="#" onclick="js_method();return false;" This method returns false after clicking and executing the js function, the page does not jump, and the page remains at the current position after execution. I looked at Taobao's homepage. They used the second method, while Alibaba's homepage used the first method. The difference between them is that each javascript method in the href is surrounded by try and catch. To sum up, the most appropriate method to call js function in a is recommended: Copy code The code is as follows:a href="javascript:void(0);" onclick="js_method()" a href="javascript:;" onclick="js_method()" a href="#" onclick="js_method();return false;" |
<<: A brief discussion on Flink's fault-tolerant mechanism: job execution and daemon
>>: Detailed explanation of JavaScript's garbage collection mechanism
【author】 Liu Bo: Senior Database Manager at Ctrip...
This article shares the specific code of Vue to a...
This article shares the specific code of JavaScri...
Table of contents Introduction to utf8mb4 UTF8 by...
This article explains how to install MySQL from a...
MySQL official website zip file download link htt...
Last time, a very studious fan asked if it was po...
The transaction log records the operations on the...
CSS transformations, while cool, have not yet bee...
Table of contents Overview 1. RangeError 2. Refer...
1. Download MySQL Community Server 5.6.35 Downloa...
Floating elements cause their parent elements to ...
This article shares with you the installation of ...
Docker download address: http://get.daocloud.io/#...
This article shares the specific code for JavaScr...