调用 JavaScript 方法的正确方法
我应该按如下方式调用 JavaScript 方法吗?
onClick="Javascript:MyMethod();"
或者我可以这样称呼它:
onClick="MyMethod();"
有什么区别吗?
Is there a reason why I should call a JavaScript method as follows?
onClick="Javascript:MyMethod();"
Or can I just call it like this:
onClick="MyMethod();"
Is there any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
onclick
属性的值是代码,而不是 URI,因此这是正确的(尽管不是唯一的方法,请参阅下面的切线 #1):这是不正确的 但基本上无害:
有时人们认为后者使用
javascript
协议,例如链接,但事实并非如此。它完全在做别的事情。onclick
属性中的代码语言是在页面级别定义的(默认为 JavaScript),因此您实际上要做的是在 JavaScript 中声明一个标签代码,然后调用MyMethod
。 JavaScript 有标签(参见下面的切线#2),尽管它们使用得不多。onclick
属性与链接上的href
属性完全不同:由于我们将代码放在需要 URI 的位置,因此我们必须使用 < code>javascript 协议,因此浏览器知道我们在做什么。
javascript
是一种协议(如http
或mailto
),Brendan Eich(JavaScript 的创建者)足够聪明地定义和注册(和实现)非常非常早,所以它得到了很好的支持。最后:最好使
onclick
全部小写,而不是混合大小写,尽管只有在使用 XHTML 时才真正很重要。切线 #1
也许有点偏离主题,但是:使用 HTML 属性来连接处理程序是完全有效的,并且跨浏览器运行良好,但它会将 JavaScript 事件连接与 HTML 混合在一起。有些人认为这是一件好事,而其他人则属于“不引人注目的 JavaScript”方面,并认为您应该稍后将所有内容连接起来。你做什么取决于你。当您的 HTML 设计人员和 JavaScript 编码人员不是同一个人时(大型团队中经常发生这种情况),这种不引人注目的方法特别有用。
不显眼的方法基本上是说:不要为此使用 HTML 属性,稍后从脚本中执行。因此,
您可能不会使用此 HTML:
与此 JavaScript 结合使用:
...其中
setTab
使用上下文来确定单击了哪个选项卡并采取相应的操作,并调用hookUpTabs
一旦 DOM 准备好。请注意,在设置点击处理程序时,我们将函数引用(而不是字符串)分配给选项卡 div 上的onclick
。我实际上不会在上面使用
onclick
,我会通过addEventListener
(标准)/attachEvent
(微软)函数使用 DOM2 处理程序。但我不想讨论标准与微软的问题。如果您开始使用不引人注目的 JavaScript,那么您也不需要使用库来为您处理这些内容(jQuery,< a href="http://prototypejs.org" rel="noreferrer">原型,闭包,随便)。切线 #2
另一个稍微偏离主题的切线:那么,这些 JavaScript 标签是什么?规范中的详细信息一如既往,但以下是在循环中使用带有定向
break
语句的标签的示例:实例
The value of an
onclick
attribute is code, not a URI, so this is correct (though not the only way you might do it, see tangent #1 below):This is incorrect but largely harmless:
Sometimes people think the latter is using the
javascript
protocol, like on links, but it isn't. It's doing something else entirely. The language of the code in theonclick
attribute is defined at page level (and defaults to JavaScript), and so what you're actually doing is declaring a label in your JavaScript code, and then callingMyMethod
. JavaScript has labels (see tangent #2 below), though they're not used much.The
onclick
attribute is totally different from thehref
attribute on links:There, since we're putting code where a URI is expected, we have to specify a URI using the
javascript
protocol, so the browser knows what we're doing.javascript
is a protocol (likehttp
ormailto
) that Brendan Eich (creator of JavaScript) was clever enough to define and register (and implement) very, very early on so it's well-supported.Finally: Best to make
onclick
all lower case, not mixed case, although it only really matters if you use XHTML.Tangent #1
Perhaps a bit off-topic, but: Using the HTML attributes for hooking up handlers is perfectly valid and works well cross-browser, but it intermixes your JavaScript event hookup with your HTML. Some people see that as a good thing, others belong to the "unobtrusive JavaScript" side and think you should hook everything up later. What you do is up to you. The unobtrusive approach is particularly useful when your HTML designers and your JavaScript coders are not the same people (as happens frequently on large teams).
The unobtrusive approach basically says: Don't use the HTML attributes for this, do it later from script. So instead of
you might have this HTML:
combined with this JavaScript:
...where
setTab
uses context to figure out which tab was clicked and act accordingly, andhookUpTabs
is called as soon as the DOM is ready. Note that where we're setting up the click handler, we're assigning a function reference, not a string, to theonclick
on the tab div.I wouldn't actually use
onclick
in the above, I'd use DOM2 handlers via theaddEventListener
(standard) /attachEvent
(Microsoft) functions. But I didn't want to get into standard vs. Microsoft stuff. And you don't either, if you start doing unobtrusive JavaScript, use a library to handle that stuff for you (jQuery, Prototype, Closure, whatever).Tangent #2
Another mildly off-topic tangent: So, what are these JavaScript labels then? Details in the spec as always, but here's an example of using labels with a directed
break
statement in a loop:Live Example
这是一个不引人注目的替代方案。内联 JavaScript 代码使您的 HTML 难以理解。将 JavaScript(逻辑)与 HTML(表示)分开是正确的方法。在 jQuery 中,它会是这样的:
将其放入您的头部或外部脚本文件中。最好是定义
MyMethod
的位置。现在您的元素是干净的,并且所有方法调用都位于同一位置,而不是通过 HTML 分散。Here is an unobtrusive alternative. Inline JavaScript code makes your HTML difficult to follow. Separating your JavaScript (logic) from your HTML (presentation) is the way to go. In jQuery it would be something like this:
Put that in your head or an external script file. Preferably where your
MyMethod
is defined. Now your element is clean, and all of your method calls are located in the same place, rather than spread out through the HTML.不同的是,第一个是错误的。
第一个恰好可以工作,因为
someidentifier:
恰好是 JavaScript 中标签的语法,但由于那里不需要标签,所以没有必要添加它。The difference is that the first one is wrong.
The first one just happens to work because
someidentifier:
happens to be the syntax for a label in JavaScript, but since there is no need for a label there, there is no point of adding it.onclick
已经假定您正在执行 JavaScript 代码,因此不需要javascript:
伪 URL。onclick
already presumes you're executing JavaScript code, so there's no need for ajavascript:
pseudo-url.AFAIK,您在这里定义了一个名为
Javascript
的跳转标签。你不应该这样做。这对通话没有影响。这是要走的路:AFAIK, you are defining a jump label with the name
Javascript
here. You shouldn't do that. This makes no difference to the call. This is the way to go: