[英]syntax difference in javascript function calling?
What is the difference between: 有什么区别:
<div onclick="return function()"></div>
vs VS
<div onclick="function()"></div>
They seem to be doing the same thing for me and I am not sure which one to use. 他们似乎对我做了同样的事情,我不确定使用哪一个。
Take for example <form onsubmit="return validate();">...
The submit will be cancelled if validate() returns false. 例如,
<form onsubmit="return validate();">...
如果validate()返回false,则提交将被取消。
With <form onsubmit="validate();">...
The submit will continue regardless of the return value of validate(). 使用
<form onsubmit="validate();">...
无论validate()的返回值如何,提交都将继续。
One will return the value to the element which the attribute resides in, the other will not.
一个将值返回到属性所在的元素,另一个不会。
When you use return function ()
the default click-method of the element will only be executed if function ()
evaluates to a value that implicitly is converted to true . 当您使用
return function ()
,只有当function ()
计算为隐式转换为true的值时,才会执行元素的默认click方法。
If it yields false the evaluation of the click-event will halt after running the onclick -property. 如果它产生false ,则在运行onclick -property后,click-event的评估将停止。
In case of onclick="function ()"
the default click-propery of the element will always be executed, no matter what the function returns. 在
onclick="function ()"
的情况下,无论函数返回什么,都将始终执行元素的默认click-propery。
function some_func () { return false; }
<a href="http://google.com" onclick="return some_func()">
link #1
</a> <!-- user will never end up at google -->
if javascript is enabled in the browser, of course.. 如果在浏览器中启用了javascript,当然..
<a href="http://google.com" onclick="some_func()">
link #1
</a> <!-- user will always end up at google -->
There is a difference, if you write return, it will check if the return is true or false and will only take action if return is true. 有一点不同,如果你写return,它将检查返回是真还是假,只有当return为true时才会采取行动。 otherwise it will just process.
否则它只会处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.