js

Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值

void 操作符用法格式如下:
1. javascript:void (expression)
2. javascript:void expression

<a href="javascript:void(0);" />

<a href="javascript:;" />

它俩点击a标签都不会执行其他操作,也不会刷新页面。 

使用void(0)是用来代替undefined,因为undefind不是JS的保留字。(保留字和关键字可以参考网址:https://www.cnblogs.com/mandy-dyf/p/4547166.html) 

在某些浏览器下undefind的值可能会被修改 (IE5之前) 。

其实void(0)或者void 0 返回结果都是undefined。

并且下面所有的结果都是undefined: 

console.log(void 0);  //undefined:

console.log(void(0));   //undefined:

console.log(void ‘hello word!’);   //undefined:

console.log(void(true));  //undefined:

一般在写A标签的时候,

格式是<a href="#">链接文字</a>,但是点击a链接的时候会自动跳转到页面的顶部,

如果写成<a href="javascript:void(0)">链接文字</a>,就不会跳转,不会发生任何问题,等后面写完后添加连接的时候改成跳转后的地址就行了。

原文地址:https://www.cnblogs.com/slightFly/p/11558475.html