2010-11-19 135 views
6

I have numerous anchor tags on my page that only trigger jQuery actions on the same page.替代<a href="#"> when the anchor tag only triggers a jQuery action without redirecting the user?

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

+0

您是否找到解决方案或者我的答案是您实施的?如果是这样的话,如果你可以把它标记为解决方案,那将会很好。 :-) – 2011-09-29 20:28:32

回答

0
return false; 

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

1

# 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 '#' 
    }) 
1

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.

+0

* href =“javascript:”*在更新版本的Firefox中打破。我不确定他们什么时候改变了这一点,但我希望有另一种选择,因为*#*通过将页面视图跳转回顶部来破坏UI。 – Typel 2015-05-15 21:59:38

+1

'javascript:void(0)' – 2016-10-10 16:18:01

9

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

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

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"),这样你就不会搞乱屏幕阅读器(和浏览器标签)。

但是,如果你想继续使用<a href="#">...</a>,只需附加一个处理程序,他们调用event.preventDefault();或返回false

1

如果锚没有用于没有JavaScript的人,那么他们根本不应该看到它。

最好的选择是在页面加载时使用jQuery生成这些链接 - 这样,只有看到它们的人才会使用它们。

在这种情况下,有href="#"是好的,因为只要你的事件处理程序return false;那么href将永远不会被你真正应该使用JS-只有行动<button>元素其次

5

完成。他们有一个默认的动作(如果在表单内),但在表单之外,它们纯粹是用于你的JS事件处理程序绑定的用户触发动作。

+1

如果'button'没有'type'属性,那么'button'有一个默认的表单行为,我真的很惊讶,然而,这似乎是当前的草稿spec说:http://www.w3.org/TR/html5/the-button-element.html#the-button-element我一直以为''button'在'button'元素上的缺省值是'button' ,而不是'submit'。谢谢。 – 2010-11-19 12:52:51

1

您是否尝试省略href参数?

<a>Link title</a> 

这应该工作得很好,不会引起浏览器加载网页或更改页面位置。事实上,除非你有一些JavaScript来支持它,否则它什么都不会做。

href参数是可选的,所以为什么包括它,如果你不使用它?

+0

锚的样式(实际上是任何html实体)与它所包含的参数无关。不带href的看起来与 RAC 2016-09-29 23:19:13

+0

完全一样。它不适用于Chrome。我没有在其他浏览器中测试过,所以我可能是错的*耸耸肩* – williamle8300 2016-09-30 00:45:30

0

我使用下面的代码,工作正常。

<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 
相关问题