本文介绍了的onclick = QUOT;的javascript:函数()" vs. onclick =" func()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<input type="submit" value="Register" onclick="javascript:submitTheForm();">

<input type="submit" value="Register" onclick="submitTheForm();">

我应该在JS之前使用 javascript:函数调用?

Should I use javascript: before a JS function call?

推荐答案

javascript:在 onwhatever =处理程序是无操作。此属性的内容始终为JavaScript,因此 javascript:定义 - 但在开关块之外或循环(您可以将其用于 break / 继续到标签的位置)它什么都不做。

javascript: inside an onwhatever="" handler is a no-op. The content of this attribute is always JavaScript so javascript: defines a label - but outside of a switch block or a loop (where you can use it to break/continue to the position of the label) it does nothing.

然而,使用不建议使用内联事件处理程序,有 注册事件处理程序。

However, using inline event handlers is discouraged, there are better ways to register event handlers.

虽然问题中没有提到,但值得注意的是在 href 属性中使用 javascript:确实有效(并且在那里它实际上是必要的)但由于各种原因非常气馁:

While not mentioned in the question, it's worth noting that using javascript: in the href attribute does work (and in there it is actually necessary) but is highly discouraged for various reasons:


  • 不指向元素

  • 调用函数r eturning a value会导致浏览器离开网站

  • 点击带有禁用JavaScript中断的链接

  • 尝试在新窗口/选项卡中打开链接休息

  • this does not point to the element
  • Calling a function returning a value causes the browser to leave the site
  • Clicking the link with disabled JavaScript breaks
  • Trying to open the link in a new window/tab breaks

因此,如果你真的必须使用内联事件,请始终使用 onclick =等没有 javascript:

So, if you really have to use inline events, always use onclick="" etc. without javascript:.

这篇关于的onclick = QUOT;的javascript:函数()&QUOT; vs. onclick =&quot; func()&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:05