当锚标记只触发 jQuery 操作而不重定向用户时?" />

替代 当锚标记只触发 jQuery 操作而不重定向用户时?

我的页面上有许多锚标记,它们仅触发同一页面上的 jQuery 操作。

不会将用户重定向到另一个位置,这是锚标记的正常预期行为。

我不想在我的应用程序中为每个操作都提供静态 URL。但是,我也不喜欢每次用户单击其中一个时都将用户发送到页面顶部> 标签。

除了 # 之外,在锚标记的 href 值中放入什么值更好?

I have numerous anchor tags on my page that only trigger jQuery actions on the same page.

The don't redirect the user to another location, which is the normal expected behavior of an anchor tag.

I don't want to have restful urls in my app for every action. But, I also don't like sending the user to the top of the page every time they click on one of these <a href="#"> tags.

What's a better value to put inside the href value of the anchor tag besides #?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

淡看悲欢离合 2024-10-10 07:46:11

您能否引用页面上的一个片段来作为未执行的 JavaScript 操作的逻辑后备?如果是这样,那么引用 并在页面上拥有一个可以工作的 id="account" 就很有意义了作为后备。

另一种选择是直接引用动态加载的内容;这样您就可以非常方便地在 Ajax 调用中使用 href,而不用以某种方式对 JavaScript 中请求的内容进行硬编码;

最后,您根本不能在 HTML 中静态地使用 ,而是使用 jQuery 动态创建它,因为它只由 jQuery 使用。如果占位符仅由 JavaScript 使用或用于 JavaScript,则无需使用 JavaScript 占位符污染标记。

关于“将用户发送到页面顶部”;您应该从连接为 click() 处理程序的函数中返回 false

$('a').click(function() {
    // Do your stuff.
    // The below line prevents the 'href' on the anchor to be followed.
    return 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 an id="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 a click() handler;

$('a').click(function() {
    // Do your stuff.
    // The below line prevents the 'href' on the anchor to be followed.
    return false;
});
无力看清 2024-10-10 07:46:11

您确实应该使用

You should really be using a <button> element for JS-only actions. They have a default action (if inside a form, or with a form 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.

旧时模样 2024-10-10 07:46:11

我使用下面的代码,效果很好。

<div class="panel-heading">Definition
          <div class="pull-right">
              <i class="fa fa-wrench"></i> 
                 <a id="step3Ad" href="javascript:void(0);">Advanced</a>
          </div>
    </div

I use below code and that works fine.

<div class="panel-heading">Definition
          <div class="pull-right">
              <i class="fa fa-wrench"></i> 
                 <a id="step3Ad" href="javascript:void(0);">Advanced</a>
          </div>
    </div
小猫一只 2024-10-10 07:46:11

如果您有不是链接的链接,也许它们不应该是链接? :-) 您可能会考虑使用不具有默认行为(在表单之外)的不同 UI 元素,例如 button (您可以在很大程度上设置按钮的样式)。或者您可以使用 span 或类似的,但如果您确实设置了适当的辅助功能信息(例如 ARIA role="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 use span or similar, but if you do be sure to set the appropriate accessibility information (such as an ARIA role="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 calls event.preventDefault(); or returns false.

我是有多爱你 2024-10-10 07:46:11

我更喜欢使用:

<a href="javascript:">foo</a>

除非它实际上是一个 ajax 调用来加载部分模板,在这种情况下我使用类似这样的东西:

<a href="/link/to/page" onClick="ajax_request_to_partial(); return false;">foo</a>

通过在 onclick 事件中返回 false,您可以确保站点不会重新加载,但它仍然可以用于在新页面中打开 url。

I prefer to use:

<a href="javascript:">foo</a>

unless it is actually an ajax call to load a partial template in which case I use something like this:

<a href="/link/to/page" onClick="ajax_request_to_partial(); return false;">foo</a>

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.

金橙橙 2024-10-10 07:46:11

您是否尝试过省略 href 参数?

<a>Link title</a>

这应该可以正常工作,并且不会引起浏览器加载网页或更改页面位置。事实上,除非你有一些 JavaScript 支持,否则它不会做任何事情。

href 参数是可选的,那么如果您不使用它为什么要包含它呢?

Have you tried omitting the href parameter?

<a>Link title</a>

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?

往事随风而去 2024-10-10 07:46:11

# 没问题。但触发的回调应该返回 false

   // assigning a click callback to all anchors
   $('a').click(function(evt){
       //... do stuff
       return false; // avoid jump to '#'
   })

# is fine. But the callbacks that are triggered should then return false.

   // assigning a click callback to all anchors
   $('a').click(function(evt){
       //... do stuff
       return false; // avoid jump to '#'
   })
夜夜流光相皎洁 2024-10-10 07:46:11

如果锚点对于没有 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 with return false; then the href will never be followed

ぺ禁宫浮华殁 2024-10-10 07:46:11
return false;

用它来阻止浏览器将它们发送到页面顶部?

return false;

Use this to stop browser sending them to top of page?

初见你 2024-10-10 07:46:11

如果您在点击时绑定一些操作,则可以调用 PreventDefault() 以避免将用户发送到页面顶部

$("a.clickable").live('click',function(){$(this).doSome(); $(this).preventDefault()});

if you bind some action on click on you can call preventDefault() to avoid sending user in top of page

$("a.clickable").live('click',function(){$(this).doSome(); $(this).preventDefault()});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文