87,915
社区成员
发帖
与我相关
我的任务
分享
void
The void operator returns undefined for any value. This is typically used to avoid outputting a value
that shouldn’t be output, such as when calling a JavaScript function from an HTML <a> element. To do
this properly, the function cannot return a valid value; otherwise the browser erases the page and displays
only the result of the function. For example:
<a href=”javascript:window.open(‘about:blank’)”>Click Me</a>
If you place this line of code into an HTML page, and click the link, you see “[Object]” printed on the
screen (Figure 2-3). This occurs because window.open() returns a reference to the newly opened window
(this and other methods of the window are discussed further in Chapter 5, “JavaScript in the
Browser”). That object is then converted to a string for display.
To avoid this, use the window.open() call with the void operator:
<a href=”javascript:void(window.open(‘about:blank’))”>Click Me</a>
This makes the window.open() call return undefined, which is not a valid value and is not displayed
in the browser window. Remember, functions that have no return value actually return undefined.
<a href="javascript:void(0)">该连接无任何操作,和普通文本效果一样。只会显示一个鼠标手型样式
如果换成#号,在本地文件夹中打开后点击链接,会打开当前文件夹。在应用服务器中http方式打开的
会是当前页面,有些浏览器会连接到当前页面,相当于刷新一次。有些则无然后操作
<a href="javascript:void(0)">
<a href="javascript:return fasle;">效果类似