帮我理解 javascript:void(null)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2863878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help me understand javascript:void(null)
提问by Anil Namde
can someone please help me with this javascript:void(null) I found it used in link buttons as follows
有人可以帮我解决这个 javascript:void(null) 我发现它在链接按钮中使用如下
<a onclick="ProcessResponse()" href="javascript:void(null)" >Accept Data</a>
采纳答案by Sarfraz
Basically what happens is the onclickthe function ProcessResponse()is called and the hrefis set to javascript:void(null)to disable the default behaviourof the link.
基本上发生的是调用onclick函数ProcessResponse()并href设置javascript:void(null)为禁用链接的默认行为。
Most developers are simply used to writing this too:
大多数开发人员也只是习惯于这样写:
<a onclick="ProcessResponse(); return false;" href="#" >Accept Data</a>
Example:
例子:
Suppose we have this link in place:
假设我们有这个链接:
<a onclick="ProcessResponse(); return false;" href="http://www.google.com" >Accept Data</a>
Note that hrefis set to www.google.combut when you actually click on that link, it would simply call the ProcessResponse()function and won't go to www.google.combecause the return falseput after ProcessResponse()disables the default behavior of the link that is going to www.google.com. Same is the case for the link you have posted.
请注意,href设置为www.google.com但当您实际单击该链接时,它只会调用该ProcessResponse()函数而不会转到,www.google.com因为return falseput afterProcessResponse()禁用了将要转到的链接的默认行为www.google.com。您发布的链接也是如此。
回答by Andy E
voidis a JavaScript operator but is sometimes mistaken for a function because of the common use of brackets that follow it. The intended purpose of voidis to evaluate an expression without returning a value. Therefore, any expression at all can be voided, it doesn't have to be nulland quite often you see void(0)or less frequently, void 0.
void是一个 JavaScript 运算符,但有时会被误认为是函数,因为它后面经常使用方括号。的预期目的void是在不返回值的情况下评估表达式。因此,任何表达式都可以被void编辑,它不一定是null你经常看到void(0)或不经常看到的,void 0.
When you use javascript:in a hrefattribute, the expression following will be evaluated and its result will be returned. This can be seen by entering the following into your browser address box:
当您javascript:在href属性中使用时,将评估以下表达式并返回其结果。这可以通过在浏览器地址框中输入以下内容来查看:
javascript:prompt("test");
Type anything into the box that appears and press enter/click ok. You will notice that the page will disappear and whatever you typed will appear. Now watch what happens if we add void 0;:
在出现的框中输入任何内容,然后按回车键/单击确定。您会注意到该页面将消失,您输入的任何内容都会出现。现在看看如果我们添加会发生什么void 0;:
javascript:prompt("test"); void 0;
After clicking OK in the prompt, nothing happens. That's void 0's handywork, it returns undefinedand so the browser does nothing about it. This applies to hrefin links too (feel free to try it). The whole thing could even just be written as javascript:void prompt("test");.
在提示中单击确定后,没有任何反应。这很void 0方便,它会返回undefined,因此浏览器不做任何处理。这也适用于hrefin 链接(请随意尝试)。整个事情甚至可以写成javascript:void prompt("test");.
As others have mentioned, it's best to use return false;from an event handler rather than using voidin the href. In fact, it's recommended not to use javascript:in the hrefattribute at all.
正如其他人所提到的,最好return false;从事件处理程序中使用void,而不是在 href 中使用。实际上,建议根本不要javascript:在href属性中使用。
回答by Jamiec
javascript:void(null)= do nothing.
javascript:void(null)= 什么都不做。
Notice there is a javascript call in the onclick event handler - that does something (I'm guessing it accepts data by processing the response ;).
请注意,onclick 事件处理程序中有一个 javascript 调用——它做了一些事情(我猜它通过处理响应来接受数据;)。
回答by Tgr
Appending void(0)to javascript instructions is a common trick when you use a javascript:pseudo-URL to run code. If you omit doing this, and the script returns something other than undefined, it will be treated as if it was passed to document.write- that is, the browser will navigate away to an empty page.
void(0)当您使用javascript:伪 URL 运行代码时,附加到 javascript 指令是一个常见的技巧。如果您省略此操作,并且脚本返回undefined以外的内容,它将被视为已传递给document.write- 也就是说,浏览器将导航到一个空页面。
There are valid applications for this trick (namely, bookmarklets should always end like this), but in the example you gave it is just wrong, for reasons already explained by others.
这个技巧有有效的应用程序(即书签应该总是这样结束),但在你给出的例子中它是错误的,原因已经被其他人解释过了。

