将JavaScript代码放入< a>中的不同方法之间的区别是什么? [英] What is the difference between the different methods of putting JavaScript code in an <a>?
问题描述
我已经看到以下方法将JavaScript代码放入< a>
标记中:
I have seen the following methods of putting JavaScript code in an <a>
tag:
function DoSomething() { ... return false; }
-
< a href =javascript :; onClick =return DoSomething();>链接< / a>
< a href =javascript:DoSomething );> link< / a>
< a href =javascript:void(0); onClick =return DoSomething();> link< / a>
< a href =# return DoSomething();> link< / a>
<a href="javascript:;" onClick="return DoSomething();">link</a>
<a href="javascript:DoSomething();">link</a>
<a href="javascript:void(0);" onClick="return DoSomething();">link</a>
<a href="#" onClick="return DoSomething();">link</a>
我理解尝试放置一个有效的URL而不是仅仅是JavaScript代码,以防万一用户没有启用JavaScript。但为了本次讨论的目的,我需要假设启用了JavaScript(没有它就不能登录)。
I understand the idea of trying to put a valid URL instead of just JavaScript code, just in case the user doesn't have JavaScript enabled. But for the purpose of this discussion, I need to assume JavaScript is enabled (they can't login without it).
我个人喜欢选项2,因为它允许您看看将要运行的是什么–在调试传递给函数的参数时尤其有用。我已经使用了它很多,并没有发现浏览器问题。
I personally like option 2 as it allows you to see what's going to be run–especially useful when debuging where there are parameters being passed to the function. I have used it quite a bit and haven't found browser issues.
我读过人们推荐4,因为它给用户一个真正的链接,但是,真的,#不是真实的。它会绝对没有在哪里。
I have read that people recommend 4, because it gives the user a real link to follow, but really, # isn't "real". It will go absolutely no where.
当你知道用户启用了JavaScript时,有没有不支持或者真的很糟糕?
Is there one that isn't support or is really bad, when you know the user has JavaScript enabled?
相关问题: Href for JavaScript链接: #或javascript:void(0)? 。
Related question: Href for JavaScript links: "#" or "javascript:void(0)"?.
推荐答案
<a href="javascript_required.html" onclick="doSomething(); return false;">go</a>
这篇关于将JavaScript代码放入< a>中的不同方法之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!