" statement; 2. Use the "" statement; 3. , use the "" statement."/> " statement; 2. Use the "" statement; 3. , use the "" statement.">

Home >Web Front-end >Front-end Q&A >What are the several ways to call javascript methods in a tag?

What are the several ways to call javascript methods in a tag?

青灯夜游
青灯夜游Original
2022-01-27 15:31:541798browse

Method: 1. Use the "6d844cae330c97f1c6dbd3acad7ecb4e" statement; 2. Use the "1e27a89785525627624307670bd870a6" statement; 3. Use the "dbf27c68c3d1d7a262aae3f8ab2ac950" statement.

What are the several ways to call javascript methods in a tag?

The operating environment of this tutorial: windows7 system, javascript1.8.5&&HTML5 version, Dell G3 computer.

Several ways to call javascript methods in a tag (take click event as an example)

Method 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, and when the javascript: protocol is used as the href attribute of a, it will not only This causes the window.onbeforeunload event to be triggered unnecessarily, and in IE, the animated gif image will stop playing. W3C standards do not recommend executing javascript statements in href

Method 2:

<a href="javascript:void(0);" onclick="js_method()">

This method is the most commonly used method on many websites and is also the most comprehensive method, onclick The 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.

Method 3:

<a href="javascript:;" onclick="js_method()">

This method is similar to method 2, the only difference is that an empty js code is executed.

Method 4:

<a href="#" onclick="js_method()">

This method is also a very common code on the Internet. # is a method built into the tag, which represents the role of top . So using this method to click on the web page returns to the top of the page.

Method 5:

<a href="#" onclick="js_method();return false;">

This method returns false after clicking to execute the js function, the page will not jump, and it will still be in the page after execution. The current location of the page.

I took a look 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 function 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;">

[Related recommendations: JavaScript learning tutorial

The above is the detailed content of What are the several ways to call javascript methods in a tag?. For more information, please follow other related articles on the PHP Chinese website!

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