Home  >  Article  >  Web Front-end  >  What is the reason for using javascript:void(0)

What is the reason for using javascript:void(0)

WBOY
WBOYOriginal
2022-04-13 17:48:103272browse

The reason for using "javascript:void(0)" is to prevent the link from jumping to other pages, retain the link's style, and prevent the link from performing actual operations. The void keyword is used to specify that an expression is to be evaluated but no value is returned. "void(0)" means no operation is performed.

What is the reason for using javascript:void(0)

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What is the reason for using javascript:void(0)

href="javascript:void(0);" The meaning of this is to let the hyperlink execute a js function instead of To jump to an address,

and void(0) represents an empty method, that is, the js function is not executed.

Why use href="javascript:void(0);"

javascript: is a pseudo-protocol, indicating that the content of the url is executed through javascript. void(0) means no operation is performed, which will prevent the link from jumping to other pages. This is often done to retain the style of the link, but does not allow the link to perform actual operations. The most critical thing in

javascript:void(0) is the void keyword, which is a very important keyword in JavaScript. Operator specifies that an expression is evaluated but does not return a value.

The syntax format is as follows:

void func()javascript:void func()

or

void(func())javascript:void(func())

The following code creates a hyperlink that nothing will happen when the user clicks it.

Example:

<html>
<head> 
<meta charset="utf-8"> 
<title>123</title> 
</head> 
<body>
    <a href="javascript:void(0)">单击此处什么也不会发生</a>
</body>
</html>

Output result:

What is the reason for using javascript:void(0)

##[Related recommendations:

javascript video tutorial, web front end

The above is the detailed content of What is the reason for using javascript:void(0). 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