void 是JavaScript中的重要关键字,该运算符表示此函数不返回值。

void 的语法可以是以下两种之一:

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

此运算符最常见的用法是在客户端 javascript:URL。在这里,表达式 alert('Warning !!!')将执行,但是它不会重新加载到当前文档中-

<html>
   <head>      
      <script type="text/javascript">
         <!--
         //-->
      </script>   
   </head>
   
   <body>   
      <p>Click the following, This won't react at all...</p>
      <a href="javascript:void(alert('Warning!!!'))">Click me!</a>     
   </body>
</html>

运行上面代码输出

看下面的示例,以下javascript:void(0)链接没有任何作用。

<html>
   <head>   
      <script type="text/javascript">
         <!--
         //-->
      </script>      
   </head>
   
   <body>   
      <p>Click the following, This won't react at all...</p>
      <a href="javascript:void(0)">Click me!</a>      
   </body>
</html>

运行上面代码输出

void 的另一个用途是生成 undefined 值,如下所示。

<html>
   <head>      
      <script type="text/javascript">
         <!--
            function getValue() {
               var a,b,c;
               
               a=void ( b=5, c=7 );
               document.write('a=' + a + ' b=' + b +' c=' + c );
            }
         //-->
      </script>      
   </head>
   
   <body>
      <p>Click the following to see the result:</p>
      <form>
         <input type="button" value="Click Me" onclick="getValue();" />
      </form>     
   </body>
</html>

运行上面代码输出

参考链接

https://www.learnfk.com/javascript/javascript-void-keyword.html