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?

Jan 27, 2022 pm 03:31 PM
a tag

Method: 1. Use the "" statement; 2. Use the "" statement; 3. Use the "" 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();">
Copy after login

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

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

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

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

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

[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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What file is index.html? What file is index.html? Feb 19, 2024 pm 01:36 PM

index.html represents the home page file of the web page and is the default page of the website. When a user visits a website, the index.html page is usually loaded first. HTML (HypertextMarkupLanguage) is a markup language used to create web pages, and index.html is also an HTML file. It contains the structure and content of a web page, as well as tags and elements used for formatting and layout. Here is an example index.html code: &lt

How to remove the color of a tag in css How to remove the color of a tag in css Apr 25, 2024 pm 05:42 PM

To remove the inherent color of the a tag, you can use the following method: Use the CSS color property to specify the text color. Use the CSS link-color property to specify link color. Use the CSS text-decoration property to remove underline and default text color. Use the CSS hover color property to change the text color on mouseover. Use the CSS visited color property to change the text color of visited a tags.

How to jump to another page using a tag How to jump to another page using a tag Sep 11, 2023 pm 04:01 PM

a tag to create a hyperlink, and then through the a tag, you can link one page to another page. Detailed introduction: To achieve page jump, you need to fill in the URL of the target page into the href attribute. The URL can be a relative path or an absolute path. The relative path is the path relative to the current page, and the absolute path is the complete URL. address.

How to implement page jump in 3 seconds: PHP Programming Guide How to implement page jump in 3 seconds: PHP Programming Guide Mar 25, 2024 am 10:42 AM

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

How to implement PHP code to jump to a specified page How to implement PHP code to jump to a specified page Mar 07, 2024 pm 02:18 PM

When writing a website or application, you often encounter the need to jump to a specific page. In PHP, we can achieve page jump through several methods. Below I will demonstrate three common jump methods for you, including using the header() function, using JavaScript code, and using meta tags. Using the header() function The header() function is a function used in PHP to send original HTTP header information. This function can be used in combination when implementing page jumps. Below is a

What should I do if the PHP web page has Chinese garbled characters? A complete solution What should I do if the PHP web page has Chinese garbled characters? A complete solution Mar 26, 2024 pm 03:27 PM

The problem of Chinese garbled characters in PHP web pages is that Chinese characters are displayed as garbled characters in the web page display. This situation is usually caused by inconsistent encoding or the character set is not set. Solving the problem of Chinese garbled characters in PHP web pages requires starting from many aspects. The following are some common solutions and specific code examples. Set the PHP file encoding: First make sure that the encoding of the PHP file itself is UTF-8. You can set the UTF-8 encoding when saving in the editor, or add the following code to the header of the PHP file to set the encoding: &amp;l

What are the attributes of a tag? What are the attributes of a tag? Nov 24, 2023 am 09:51 AM

The a tag attributes are: 1. href; 2. target; 3. rel; 4. download; 5. hreflang; 6. type. Detailed introduction: 1. href, which defines the target URL of the link. This is the most commonly used attribute of the "<a>" tag. It can link to any part of the web page, such as the top of the page, a specific paragraph, etc.; 2. target, which defines the link in Where to open; 3. rel, defines the relationship of the link. This attribute allows specifying the relationship between the current document and the linked document, etc.

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

See all articles