理解“javascript : ... ;” <a href=" ">里面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10309198/
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
Understanding "javascript : ... ;" inside <a href=" ">
提问by pierrotlefou
What is the meanign of javascript:;
that is kept inside the href
attribute in an link?
什么是的meanignjavascript:;
是保持内部href
在链接属性?
Like
喜欢
<a href="javascript: ... ;">Link</a>
采纳答案by Starx
IF you need to pass a javascript snippetwhich needs to run instead of the default behaviorof an element then you use this javascript: ;
syntax.
如果您需要传递需要运行的 javascript片段而不是元素的默认行为,那么您可以使用此javascript: ;
语法。
For example
例如
<a href="javascript:alert('');">Test</a> <!-- Runs on the click of the link -->
Similarly, you can combine these on other events also, like onclick
, onchange
etc but this is really notnecessary, since you can execute the snippet, directly.
同样,你也可以在其他事件结合这些,比如onclick
,onchange
等,但这次真的没有必要的,因为你可以直接执行该代码段。
The uses of this, i have seen in years are:
我多年来看到的这个用途是:
<a href="javascript:void(0);">Test</a>
<form action="javascript:void(0);">..</form>
<a href="javascript:void(0);">Test</a>
<form action="javascript:void(0);">..</form>
回答by Andrew T
The javascript:; in the href does the same as putting something in the "onclick" property.
javascript:; 在 href 中与将某些内容放入“onclick”属性中的作用相同。
Thus,
因此,
<a href="javascript:do_something();">Link</a>
is identical to
等同于
<a href="#" onclick="do_something();">Link</a>
I'm not sure which is better to use when, but worth mentioning is that you can type these "links" in to the address bar of most modern browsers and it will run.
我不确定什么时候使用哪个更好,但值得一提的是,您可以在大多数现代浏览器的地址栏中键入这些“链接”,它就会运行。
copy and paste (or type, chrome seems to prohibit this.) in to your address bar
复制并粘贴(或键入,chrome 似乎禁止这样做。)到您的地址栏中
javascript:alert("test");
as well as you can save links with these addresses to bookmarks so that it will run that script on any page you click the bookmark on.
以及您可以将带有这些地址的链接保存到书签,以便它可以在您单击书签的任何页面上运行该脚本。
回答by Sudhir Bastakoti
That alone does nothing, but normally javascript:precedes some JavaScript code to indicate that the browser should execute the code instead of treat the href attribute as a URL. Thats it.
这本身什么都不做,但通常javascript:在一些 JavaScript 代码之前,以指示浏览器应该执行代码而不是将 href 属性视为 URL。而已。
回答by Eugene Ryabtsev
This might be intended as a reference that does nothing when clicked and might be accompanied by setting onclick etc. to actually do anything.
这可能旨在作为单击时什么都不做的参考,并且可能伴随着设置 onclick 等来实际执行任何操作。
upd: While some say that putting a script in href is the same as putting it in onclick, there are some differences, like what happens on right click (and some scripts definitely should not be opened in a new tab or such). Still, my opinion of the practice is that it is somewhat ugly, and not only because of uninformative text in status bar when mouse over the link.
upd:虽然有人说将脚本放在 href 中与将其放在 onclick 中相同,但还是存在一些差异,例如右键单击时会发生什么(并且某些脚本绝对不应在新选项卡等中打开)。尽管如此,我对这种做法的看法是它有点难看,不仅仅是因为当鼠标悬停在链接上时状态栏中的文本信息不足。
回答by FRD
You can imagine that as someone copying that "javascript:;" into the url box and hitting Enter. Since it starts with "javascript:" the browser will know to execute what follows as javascript in the current page. Since all that follows is a ";", nothing will happen, actually. That's the same as clicking a button that has a onClick attribute set to ";".
你可以想象有人复制了“javascript:;” 进入 url 框并按 Enter。由于它以“javascript:”开头,因此浏览器将知道在当前页面中以 javascript 的形式执行以下内容。由于后面的只是一个“;”,实际上什么都不会发生。这与单击 onClick 属性设置为“;”的按钮相同。
For example:
例如:
<a href="javascript:alert('howdy!')">Click me!</a>
has the same effect that
具有相同的效果
<button onClick="alert('howdy!')">Click me!</button>
or even
甚至
<a href="#" onClick="alert('howdy!')">Click me!</a>
Keep in mind that, since it's a link, most browsers will render its address (in that case "javascript:alert('howdy!')") in the status bar, and that may be undesired (I find it to be particularly ugly).
请记住,由于它是一个链接,因此大多数浏览器会在状态栏中呈现其地址(在这种情况下为“javascript:alert('howdy!')”),这可能是不受欢迎的(我发现它特别难看) )。