What we often use to have 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. Moreover, when the javascript: protocol is a href attribute of a, it will not only cause unnecessary triggering of the window.onbeforeunload event, but also stop playing gif animation images in IE. W3C standard does 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, while void is an operator. void(0) returns undefined, and the address does not jump. And this method will not directly expose the js method to the browser's status bar like the first method.
3.a href=javascript:; onclick=js_method()
This method is similar to two, the difference is that it executes an empty js code.
4.a href=# onclick=js_method()
This method is also a very common code on the Internet. # is a built-in method of tags, representing the role of top. So use this method to click the web page and return to the top of the page.
5.a href=# onclick=js_method();return false;
This method clicks to execute the js function and returns false, the page does not jump, and it is still at the current location of the page after execution.
I looked at the homepage of taobao. They used the second method, while the homepage of alibaba is used the first method. The difference from us is that the javascript method in each href is surrounded by try and catch.
Based on the above, it is recommended to use the most appropriate method for calling the js function in a:Copy the code