onClick="javascript: function('value')'" 和 onClick="function('value');" 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11823290/
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
What is the difference between onClick="javascript: function('value')'" and onClick="function('value');"?
提问by Jalpesh Patel
What is the difference between the following?
以下有什么区别?
onClick="javascript: function('value');"
onClick="function('value');"
onClick="javascript: function('value');"
onClick="function('value');"
When do I use the javascript:
before the function call, and why?
我什么javascript:
时候在函数调用之前使用,为什么?
Can I call the function without using javascript:
prefix?
我可以在不使用javascript:
前缀的情况下调用该函数吗?
Please help me understand the difference.
请帮助我理解其中的区别。
回答by nnnnnn
In an element's inline event handler, like onclick
, onchange
, onsubmit
, etc., you do notneed the javascript:
prefix - the reason you often see it is because people confuse it with the href
syntax that I explain below. It doesn't cause an error - I believe it is interpreted as a label- but it is unnecessary.
在一个元素的内联事件处理程序,像onclick
,onchange
,onsubmit
等等,你不是需要的javascript:
前缀-你经常看到这是因为人们与混淆的原因href
语法,我在下面解释。它不会导致错误 - 我相信它被解释为一个标签- 但它是不必要的。
It doesn't matter whether you want to call a function or run a "simple" JS statement, either way don't include javascript:
- that is, all three of these are valid:
无论您是要调用函数还是运行“简单”的 JS 语句都没有关系,无论哪种方式都不要包含javascript:
- 也就是说,所有这三个都是有效的:
onclick="doSomething('some val');"
onclick="return false;"
onclick="doSomething(); doSomethingElse(); return false;"
If you are using inline event attributes don't use the javascript:
prefix.
如果您使用内联事件属性,请不要使用javascript:
前缀。
(I say "ifyou are using inline event attributes" because this practice is really outdated: it is better to assign the event handlers using JS in a script block.)
(我说“如果你使用内联事件属性”是因为这种做法真的已经过时了:最好在脚本块中使用 JS 分配事件处理程序。)
You only need the javascript:
prefix when you want to run JavaScript from an <a>
element's href
attribute like this:
javascript:
当您想从<a>
元素的href
属性运行 JavaScript 时,您只需要前缀,如下所示:
<a href="javascript: someFunc();">Whatever</a>
That is because the browser normally expects the href
to contain a URI, so the javascript:
prefix tells it to expect JS code instead. However, I don't recommending doing that because the page won't work for people who have JS disabled. Better to include an href
that directs the user to a page telling them to enable JS, and include an onclick
to do the JS functionality:
那是因为浏览器通常期望href
包含一个 URI,所以javascript:
前缀告诉它期望 JS 代码。但是,我不建议这样做,因为该页面不适用于禁用 JS 的人。最好包含一个href
将用户引导到告诉他们启用 JS 的页面,并包含一个onclick
执行 JS 功能的页面:
<a href="/enableJS.html" onclick="doSomething(); return false;">Whatever</a>
That way the link does something useful for users whether they have JS enabled or not. The return false
at the end of the click handler prevents the default behaviour for a click (which would be to navigate to the specified URL).
这样,无论用户是否启用了 JS,该链接都会对用户有用。将return false
在单击处理防止年底点击默认行为(这将是导航到指定的URL)。
回答by Quentin
The first one has a completely redundant label.
第一个有一个完全多余的标签。
When should i have to use the "javascript:" before the function call and what is the reason of use it?
我什么时候应该在函数调用之前使用“javascript:”,使用它的原因是什么?
Never. (Well, almost. You do use it in javascript:
scheme URIs, but you should only use those if you are writing a bookmarklet, and they wouldn't appear in an onclick
attribute).
绝不。(嗯,差不多。您确实在javascript:
方案 URI 中使用了它,但是只有在编写书签时才应该使用它们,并且它们不会出现在onclick
属性中)。
(javascript:
URIs are where the cargo-cultpractice of prefixing onclick
attribute values with a label came from.)
(javascript:
URI 是用标签作为属性值前缀的货物崇拜实践的onclick
来源。)
can i call the function without using "javascript:"??
我可以在不使用“javascript:”的情况下调用该函数吗??
Yes.
是的。