如何在 JavaScript 中定义什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6424094/
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
How to define DO NOTHING in JavaScript
提问by xyz
I am displaying a confirm box in my app. When the user clicks on Okay
, I close the window
when user clicks on Cancel
, I want to do nothing but stay on the same page.
我在我的应用程序中显示一个确认框。当用户点击 时Okay
,我会
在用户点击时关闭窗口Cancel
,我只想停留在同一页面上。
What should I write in place of DO_NOTHING?
我应该写什么来代替 DO_NOTHING?
<a href="#" onclick="confirm('Do you wan to close the application ?')?window.close():DO_NOTHING')">Close the application ?</a>
If I keep it empty it does not work. If I write any random string it works but displays string undefined
error.
如果我把它留空,它就不起作用。如果我编写任何随机字符串,它可以工作但显示string undefined
错误。
回答by Matt MacLean
Try using void(0)
尝试使用 void(0)
<a href="#" onclick="confirm('Do you wan to close the application ?')?window.close():void(0)')">Close the application ?</a>
回答by Shadow Wizard is Ear For You
Just have this instead:
只是有这个:
onclick="if (confirm('Do you wan to close the application ?')) window.close();"
No point of using the x?y:z
if you don't intend to use it "fully".
没有点使用的x?y:z
,如果你不打算使用它“完全”。
回答by Ronny
void(0)
or null
would be fine, but consider using a plain if(confirm(...))
void(0)
或者null
会很好,但考虑使用普通的if(confirm(...))
回答by Thor Jacobsen
try return false;
or null
尝试return false;
或null
// What should I write in place of DO_NOTHING
<a href="#" onclick="confirm('Do you wan to close the application ?')?window.close():return false')">Close the application ?</a>
回答by Widor
return false;
should do the trick.
return false;
应该做的伎俩。
回答by Stein G. Strindhaug
I think simply return
or return false
should do.
我觉得简单return
还是return false
应该做。
I usually don't use inline javascript, if the onClick code were in a function attached to the onClick event, you would probably use Event.cancel() or similar... (If that's the actual link it would not do anything anyway if the event bubbled through, only jump to an non-existen blank anchor)
我通常不使用内联 javascript,如果 onClick 代码在附加到 onClick 事件的函数中,你可能会使用 Event.cancel() 或类似的......(如果这是实际链接,它不会做任何事情,如果事件冒泡,只跳到一个不存在的空白锚点)
回答by mihsathe
Try an empty object like this
尝试一个像这样的空对象
<a onclick="confirm('xyz')?window.close():{}">well</a>
In fact any stupit thing like :function(){}
eval()
should do the job well.
事实上,任何愚蠢的事情都:function(){}
eval()
应该做得很好。
回答by amal
condition ? function call : function call
is not how ?: works .
condition ? function call : function call
是不是怎么样?:作品。
Should be
应该
eval(condition? function call : function call/ blank string)
eval(condition? function call : function call/ blank string)
回答by Alex
Try:
尝试:
onclick="return confirm('Do you wan to close the application ?')"