2010-11-19 16 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

あなたが解決策を見つけましたか、私の答えはあなたの実装ですか?もしそれが解決策であるとすれば、それはいいと思います。 :-) –

答えて

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

+1

'javascript:void(0)' –

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を使用してこれらのリンクを生成することです。これを見るのは、それらを使用する人だけです。この場合

、あなたは本当にJS-アクションのみのため<button>要素を使用しなければならない限り、あなたのイベントハンドラはreturn false;で終了して、その後href

5

を踏襲することはありませんのでhref="#"が、大丈夫ですました。それらはデフォルトのアクション(フォーム内の場合)を持ちますが、フォーム外では、JSイベントハンドラをバインドするユーザーによってトリガーされるアクションのためのものです。

+1

'button'は' type'属性を持たないのであればデフォルトのフォーム動作を持っていますが、現在のドラフト仕様は次のとおりです:http://www.w3.org/TR/html5/the-button-element.html#the-button-element私はいつも 'button'要素の' type'に欠けている値のデフォルトは 'button' 'submit'ではなく、ありがとう。 –

1

hrefパラメータを省略しようとしましたか?

<a>Link title</a> 

これはうまく動作し、ウェブページを読み込んだりページの位置を変更したりするブラウザを誘発しません。実際には、JavaScriptをサポートしていない限り、何もしません。

hrefパラメータはオプションです。なぜなら、hrefパラメータを使用していない場合はなぜそれを含めるのですか?

+0

アンカー(実際には任意のhtmlエンティティ)のスタイリングは、それに含まれるパラメータとは関係ありません。 out hrefのは、正確には RAC

+0

と同じに見えますが、Chromeでは表示されません。私は他のブラウザでそれをテストしていないので、私は間違っているかもしれません* shrug * – williamle8300

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 
関連する問題