问题描述
我一直在努力通过阅读尽可能多的javascript代码来提高我的javascript技能。在这样做时,我有时会看到 javascript:
前缀附加到HTML元素标记中的事件处理程序属性的前面。这个前缀的目的是什么?基本上,之间有任何明显的区别:
I've been making a concerted effort to improve my javascript skills lately by reading as much javascript code as I can. In doing this I've sometimes seen the javascript:
prefix appended to the front of event handler attributes in HTML element tags. What's the purpose of this prefix? Basically, is there any appreciable difference between:
onchange="javascript: myFunction(this)"
和
onchange="myFunction(this)"
?
推荐答案
你的例子中可能没什么。我的理解是 javascript:
用于锚标签(代替实际的 href
)。您可以使用它,以便您的脚本可以在用户单击链接时执行,但不会启动导航回页面(空白 href
加上 onclick
会这样做。
Probably nothing in your example. My understanding is that javascript:
is for anchor tags (in place of an actual href
). You'd use it so that your script can execute when the user clicks the link, but without initiating a navigation back to the page (which a blank href
coupled with an onclick
will do).
例如:
<a href="javascript:someFunction();">Blah</a>
而不是:
<a href="" onclick="someFunction();">Blah</a>
这篇关于“javascript:”的目的是什么(如果有的话)在事件处理程序标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!