How to call javascript in href

藏色散人
Release: 2023-01-07 11:41:28
Original
4197 people have browsed it

Href method of calling javascript: 1. Through ""; 2. Through "

The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer

How does href call javascript?

In HTML, the href attribute of the 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>
Copy after login

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

Method one:

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>
Copy after login

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>
Copy after login

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()">
Copy after login

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;">
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template