将 JavaScript 代码放入 中的不同方法有什么区别?
我见过以下将 JavaScript 代码放入 标记中的方法:
function DoSomething() { ... return false; }
我理解尝试输入有效 URL 而不仅仅是 JavaScript 代码的想法,以防万一用户没有启用 JavaScript。 但出于本次讨论的目的,我需要假设 JavaScript 已启用(没有它他们无法登录)。
我个人喜欢选项 2,因为它允许您查看将要运行的内容——在调试将参数传递给函数的情况下特别有用。 我已经使用了很多次了,还没有发现浏览器问题。
我读到人们推荐 4,因为它为用户提供了一个真正的链接,但实际上,# 并不是“真实的”。 它绝对不会去任何地方。
当您知道用户启用了 JavaScript 时,是否存在不支持或非常糟糕的情况?
I have seen the following methods of putting JavaScript code in an <a>
tag:
function DoSomething() { ... return false; }
<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>
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).
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.
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.
Is there one that isn't support or is really bad, when you know the user has JavaScript enabled?
Related question: Href for JavaScript links: “#” or “javascript:void(0)”?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我非常喜欢 Matt Kruse 的 Javascript 最佳实践文章。 在其中,他指出使用
href
部分执行 JavaScript 代码是一个坏主意。 即使您已声明您的用户必须启用 JavaScript,但您没有理由不能拥有一个简单的 HTML 页面,以便所有 JavaScript 链接都可以指向其href
部分,以防万一有人发生这种情况登录后关闭 JavaScript。我强烈建议您仍然允许这种后备机制。 像这样的事情将遵循“最佳实践”并实现您的目标:I quite enjoy Matt Kruse's Javascript Best Practices article. In it, he states that using the
href
section to execute JavaScript code is a bad idea. Even though you have stated that your users must have JavaScript enabled, there's no reason you can't have a simple HTML page that all your JavaScript links can point to for theirhref
section in the event that someone happens to turn off JavaScript after logging in. I would highly encourage you to still allow this fallback mechanism. Something like this will adhere to "best practices" and accomplish your goal:当您可以使用
addEventListener
/attachEvent
时,为什么要这样做? 如果没有href
等效项,请不要使用,而使用
Why would you do this when you can use
addEventListener
/attachEvent
? If there is nohref
-equivalent, don't use an<a>
, use a<button>
and style it accordingly.您忘记了另一种方法:
使用 JavaScript 代码:
我无法评论哪个选项具有最好的支持或哪个在语义上是最好的,但我只想说我更喜欢这种风格,因为它将您的内容与您的内容分开。 JavaScript 代码。 它将所有 JavaScript 代码保存在一起,这更容易维护(特别是如果您将其应用于许多链接),您甚至可以将其放入外部文件中,然后可以打包该文件以减少文件大小并由客户端浏览器缓存。
You forgot another method:
With the JavaScript code:
I can't comment on which of the options has the best support or which is semantically the best, but I'll just say that I much prefer this style because it separates your content from your JavaScript code. It keeps all the JavaScript code together, which is much easier to maintain (especially if you are applying this to many links), and you can even put it in an external file which can then be packed to reduce filesize and cached by client browsers.
我会这样做,或者:
视情况而定。 对于较大的应用程序,第二个是最好的,因为它可以整合您的事件代码。
I will do this, or:
Depending on the situation. For larger apps, the second one is best because then it consolidates your event code.
方法 #2 在 FF3 和 IE7 中存在语法错误。
我更喜欢方法 #1 和 #3,因为 #4 用“#”弄脏了 URI,尽管会减少输入次数...
显然,正如其他响应所指出的,最好的解决方案是将 html 与事件处理分开。
Method #2 has a syntax error in FF3 and IE7.
I prefer methods #1 and #3, because #4 dirty the URI with '#' although causes less typing...
Obviously, as noted by other responses, the best solution is separate html from event handling.
我注意到 this:
和 this:
之间的一个区别是,如果在任何一种情况下您都有:
那么对于第一个示例,
act2
将在act1
之前运行,而在第二个示例中例如,情况恰恰相反。One difference I've noticed between this:
and this:
is that if in either case you have:
then for the first example,
act2
will run beforeact1
and in the second example, it will be the other way around.仅限现代浏览器
跨浏览器
您可以在文档准备好之前运行此命令,单击按钮将起作用,因为我们将事件附加到文档。
来源:
Modern browsers only
Cross-browser
You can run this before the document is ready, clicking the buttons will work because we attach the event to the document.
Sources: