替代< a href ="#">当锚标签只触发一个jQuery动作而不重定向用户? [英] Alternative to <a href="#"> when the anchor tag only triggers a jQuery action without redirecting the user?

查看:85
本文介绍了替代< a href ="#">当锚标签只触发一个jQuery动作而不重定向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有很多定位标记,只能触发同一页面上的jQuery动作。



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



我不希望在每个操作的应用程序中拥有宁静的网址。但是,我也不喜欢每次点击这些< a href =#> 标签之一时将用户发送到页面的顶部。



除了 href

解决方案

您可以引用页面上的一个片段,它可以作为逻辑回退到非执行的JavaScript行动?如果是这样,那么引用< a href =#account> 并使 id =account<可以作为后备。



另一种选择是直接引用动态加载的内容;这样,您可以非常方便地在Ajax调用中使用 href ,而不是以某种方式硬编码JavaScript中请求的内容; < a href =/ path / to / dynamic / content> 最后,您可以在HTML中完全没有< a href =#> 静态地,但是用jQuery实时创建它,因为它只被jQuery使用。无论如何,如果占位符仅由JavaScript使用,并且无论如何都不需要使用占位符来污染标记。



关于将用户发送到页面顶部;你应该将返回false 作为你已经连接的函数作为 click()处理程序;


$ b $ pre $ $('a')。click(function(){
//做你的东西。
//在下面的行可以防止跟踪锚点上的'href'。
return false;
});


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 #?

解决方案

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;
});

这篇关于替代&lt; a href =&quot;#&quot;&gt;当锚标签只触发一个jQuery动作而不重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆