Several ways to use the a tag

 We commonly have click events in the a tag:
1. a href=" JavaScript :js_method();"

This is a common method on our platform, but this method is prone to problems when passing parameters such as this, and javascript : When the protocol is used as the href attribute of a, it will not only cause unnecessary triggering of the window.onbeforeunload event, but also stop the gif animation image from playing in IE. The 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 it is also the most comprehensive method, the onclick method Responsible for executing js functions, 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's status bar like the first method.

3.a href="javascript:;" onclick="js_method()"

This method is similar to the two methods, except 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 role of top. So use this method to return to the top of the page after clicking on the web page.

5.a href="#" onclick="js_method();return false;"



I looked at Taobao's homepage, they used the second method, and the alibaba's homepage used the first method. The difference from ours is that the javascript methods in each href are surrounded by try and catch.

Based on the above, the most appropriate method to call js functions in a is recommended:
a href="javascript:void(0);" onclick="js_method()"
a href="javascript:;" onclick="js_method()"
a href="#" onclick="js_method();return false;"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611263&siteId=291194637