Home >Web Front-end >HTML Tutorial >Several methods of calling js in a are organized and recommended for use_HTML/Xhtml_Web page production

Several methods of calling js in a are organized and recommended for use_HTML/Xhtml_Web page production

WBOY
WBOYOriginal
2016-05-16 16:40:021242browse

We commonly 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 It is easy to cause problems when passing parameters such as this, and 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 also cause the GIF animated picture to stop 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 popular for many websites The commonly used method 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 does not directly expose the js method to the status bar of the browser like the first method.

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

This method is similar to the 2 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 role of top. So using this method to click on the web page returns to the top of the page.

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

This method returns false after clicking to execute the js function, and the page will not jump. It is still at the current position of the page.

I looked at taobao’s homepage. They use the second method, while alibaba’s homepage uses the first method. The difference from ours is that the javascript method in each href uses try. , catch surrounded.

Based on the above, the most appropriate method to call js functions 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;"
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