Home > Web Front-end > JS Tutorial > body text

javascript:void(0) meaning

高洛峰
Release: 2016-12-13 13:29:29
Original
1523 people have browsed it

We often use code like javascript:void(0), so what does javascript:void(0) mean in JavaScript? The most critical thing in

javascript:void(0) is the void keyword. void is a very important keyword in JavaScript. This operator specifies to calculate an expression but does not return a value.

The syntax format is as follows:

<head>
<script type="text/javascript">
<!--
void func()
javascript:void func()

或者

void(func())
javascript:void(func())
//-->
</script>
</head>
Copy after login

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

<a href="javascript:void(0)">单击此处什么也不会发生</a>
Copy after login

void(0) evaluates to 0 when user links, but has no effect on Javascript.

In the following example, a warning message is displayed after the user clicks the link:

<head>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<a href="javascript:void(alert(&#39;Warning!!!&#39;))">点我!</a>
</body>
Copy after login

In the following example, parameter a will return undefined:

<head>
<script type="text/javascript">
<!--
function getValue(){
  var a,b,c;
  a = void ( b = 5, c = 7 );
  document.write(&#39;a = &#39; + a + &#39; b = &#39; + b +&#39; c = &#39; + c );
}
//-->
</script>
</head>
Copy after login

The difference between href="#" and href="javascript:void(0)"

# Contains a location information. The default anchor is #top, which is the top of the web page.

And javascript:void(0), just means a dead link.

When the page is very long, # will be used to locate the specific location of the page. The format is: # + id.

If you want to define a dead link please use javascript:void(0).

<a href="javascript:void(0);">点我没有反应的!</a> 
<a href="#pos">点我定位到指定位置!</a> 
<br> 
... 
<br> 
<p id="pos">尾部定位点</p>
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!