替代 当锚标记只触发 jQuery 操作而不重定向用户时?
我的页面上有许多锚标记,它们仅触发同一页面上的 jQuery 操作。
不会将用户重定向到另一个位置,这是锚标记的正常预期行为。
我不想在我的应用程序中为每个操作都提供静态 URL。但是,我也不喜欢每次用户单击其中一个时都将用户发送到页面顶部>
标签。
除了 #
之外,在锚标记的 href
值中放入什么值更好?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您能否引用页面上的一个片段来作为未执行的 JavaScript 操作的逻辑后备?如果是这样,那么引用
并在页面上拥有一个可以工作的
id="account"
就很有意义了作为后备。另一种选择是直接引用动态加载的内容;这样您就可以非常方便地在 Ajax 调用中使用
href
,而不用以某种方式对 JavaScript 中请求的内容进行硬编码;。
最后,您根本不能在 HTML 中静态地使用
,而是使用 jQuery 动态创建它,因为它只由 jQuery 使用。如果占位符仅由 JavaScript 使用或用于 JavaScript,则无需使用 JavaScript 占位符污染标记。
关于“将用户发送到页面顶部”;您应该从连接为
click()
处理程序的函数中返回 false
;Can you reference a fragment on the page that could work as a logical fallback to the non-executed JavaScript action? If so, it makes a lot of sense to reference
<a href="#account">
and have anid="account"
on the page that could work as a fallback.Another option is to reference the dynamically loaded content directly; that way you can quite conveniently use the
href
in the Ajax call instead of hard-coding what to request in JavaScript somehow;<a href="/path/to/dynamic/content">
.Lastly, you can not have the
<a href="#">
statically in the HTML at all, but instead create it on the fly with jQuery since it's only used by jQuery anyway. No need to pollute the markup with placeholders for JavaScript if the placeholders are only used by and for JavaScript anyway.Regarding "sending the user to the top of the page"; you should just
return false
from your the function you have hooked up as aclick()
handler;您确实应该使用
You should really be using a
<button>
element for JS-only actions. They have a default action (if inside a form, or with aform
attribute that associates them with a form) but without an attached form they’re purely for user triggered actions that you bind your JS event handler to.如果您有不是链接的链接,也许它们不应该是链接? :-) 您可能会考虑使用不具有默认行为(在表单之外)的不同 UI 元素,例如
button
(您可以在很大程度上设置按钮的样式)。或者您可以使用span
或类似的,但如果您确实设置了适当的辅助功能信息(例如 ARIArole="link"
),这样您就不会弄乱屏幕阅读器(和浏览器选项卡)。但如果您想继续使用
...
,只需为它们附加一个调用event.preventDefault() 的处理程序即可;
或返回false
。If you have links that aren't links, perhaps they shouldn't be links? :-) You might consider a different UI element that doesn't have a default behavior (outside of a form), like
button
(you can style buttons to a substantial degree). Or you could usespan
or similar, but if you do be sure to set the appropriate accessibility information (such as an ARIArole="link"
) so that you don't mess up screen readers (and browser tabbing).But if you want to continue to use
<a href="#">...</a>
, just attach a handler to them that callsevent.preventDefault();
or returnsfalse
.我更喜欢使用:
除非它实际上是一个 ajax 调用来加载部分模板,在这种情况下我使用类似这样的东西:
通过在 onclick 事件中返回 false,您可以确保站点不会重新加载,但它仍然可以用于在新页面中打开 url。
I prefer to use:
unless it is actually an ajax call to load a partial template in which case I use something like this:
By returning false in the onclick event, you make sure the site is not reloaded, but it can still be used for opening the url in a new page.
您是否尝试过省略 href 参数?
这应该可以正常工作,并且不会引起浏览器加载网页或更改页面位置。事实上,除非你有一些 JavaScript 支持,否则它不会做任何事情。
href 参数是可选的,那么如果您不使用它为什么要包含它呢?
Have you tried omitting the href parameter?
That should work just fine and not provoke the browser to load a webpage or change the pages position. In fact it won't do anything unless you have some JavaScript to support it.
The href parameter is optional so why include it if you aren't using it?
#
没问题。但触发的回调应该返回 false
。#
is fine. But the callbacks that are triggered should thenreturn false
.如果锚点对于没有 JavaScript 的人来说毫无用处,那么他们根本不应该看到它。
最好的替代方法是在页面加载时使用 jQuery 生成这些链接 - 这样,只有那些会使用它们的人才能看到它们。
在这种情况下,使用
href="#"
就可以了,因为只要您的事件处理程序以return false;
结束,那么href
就会永远不会被跟随If the anchor is useless to people who don't have javascript, then they shouldn't see it at all.
The best alternative is to generate these links using jQuery on page load - that way the only people who see them are those who will use them.
In this case, having
href="#"
is fine, because as long as your event handler finishes withreturn false;
then thehref
will never be followed用它来阻止浏览器将它们发送到页面顶部?
Use this to stop browser sending them to top of page?
如果您在点击时绑定一些操作,则可以调用 PreventDefault() 以避免将用户发送到页面顶部
if you bind some action on click on you can call preventDefault() to avoid sending user in top of page