当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript:void(0)的真正含义实例分析

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript:void(0)的真正含义实例分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 181 ::
收藏到网摘: n/a

void操作符解释 大家看JavaScript脚本经常可以看到这样的代码:
〈a href="javascript:doTest2();void(0);"〉here〈/a〉
但这儿的void(0)究竟是何含义呢?
JavaScript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值。
void 操作符用法格式如下:
1. javascript:void (expression)
2. javascript:void expression
expression 是一个要计算的 JavaScript 标准的表达式。表达式外侧的圆括号是可选的,但是写上去是一个好习惯。
你可以使用 void 操作符指定超级链接。表达式会被计算但是不会在当前文档处装入任何内容。
下面的代码创建了一个超级链接,当用户点击以后不会发生任何事。当用户点击链接时,void(0) 计算为 0,但在 JavaScript 上没有任何效果。
void和#的区别
#包含了一个位置信息默认的锚点是#top 也就是网页的上端,而javascript:void(0) 仅仅表示一个死链接。
这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首,而javascript:void(0) 则不是如此所以调用脚本的时候最好用void(0)。