" method; 2. Through the " " method; 2. Through the "

Home  >  Article  >  Web Front-end  >  How to call javascript in href

How to call javascript in href

藏色散人
藏色散人Original
2021-07-01 10:19:014134browse

Href method of calling javascript: 1. Through "2882ee5ee88f853351d77f4c846ce991"; 2. Through "0a24ec122726c0f8784a145dc9065e81 tag is used to specify the URL of the target of the hyperlink.

The example is as follows:

<p>
<a href="/index.html">本文本</a> 是一个指向本网站中的一个页面的链接。</p>
<p><a href="http://www.microsoft.com/">本文本</a> 是一个指向万维网上的页面的链接。</p>

When the 3499910bf9dac5ae3c52d5ede7383485 tag calls the code in JS, there are the following methods:

Method one:

<a href = "javascript:js_method()">文本</a>

This is a commonly used method on the platform, but this method is prone to problems when passing parameters such as this, and the javascript: protocol as the href attribute of a will not only cause unnecessary triggering of the window.onbeforeunload event, but also cause unnecessary triggering of the window.onbeforeunload event in IE. It will stop the gif animation from playing. W3C standards do not recommend executing javascript statements inside href.

Method 2:

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

This is the most commonly used method on many websites, and it is also the most comprehensive method. onclick is responsible for executing js functions, and void is an operator. void(0) returns undefined , 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 three:

<a href = "javascript:;" onclick = "js_merthod()">文本</a>

This method is similar to the second method, 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, representing the role of top. So this method returns to the top of the page after clicking on the web page.

Method Five:

<a href="#" οnclick="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 at the current position of the page after execution.

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to call javascript in href. 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