开发者

JavaScript inline events syntax

Is there any reason to use one of the following more than the others:

<input type="button" value="b1" onclick="manageClick(this)" />
<input type="button" value="b2" onclick="manageClick(this);" />
<input type="button" value="b2" onclick="manageClick(this);return false;" />
<input type="button" value="b3" onclick="return manageClick(this);" />
<input type="button" value="b4" onclick="javascript:return manageClick(this开发者_开发百科);" />

And please do not spend your valuable time to tell me to use jQuery or attachEvent/addEventListener. It's not really the objective of my question.


There's no difference at all between the first two, in this specific situation the semicolon is optional.

The third one will prevent any default action from occurring, which the first two won't.

The fourth will prevent the default action or not depending on the return value of manageClick.

The fifth one is incorrect.

(And wherever suitable, use attachEvent/addEventListener -- ducks and runs)


<input type="button" value="b3" onclick="return manageClick(this);" />

edit Return is preferable in cases where you wish to control element behaviour, such as for <a> and <input type="submit">, so assuming this is the case, above is your answer, if not just omit the return and just go with onclick="manageClick(this);".

Also, have a look at http://crisp.tweakblogs.net/blog/313/the-useless-javascript-pseudo-protocol.html regarding the use of javascript:. ^_^

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜