2012-04-13 27 views
0

Is there any way to stop the page from loading the next page when someone clicks on a <a> tag instead i want it to give the href value so for example "www.google.com" and then do a jquery .load() which is like thisデフォルトの削除<a href> action

$(".window").load(hrefvalue); 

i know i could just change all the href values to fire up a javascript function but that takes a bit of time so im just looking for the easiest way.

so i want it to do

  1. stop the page loading the next part on a <a href click.
  2. get the href value e.g http://www.google.comを削除します。
  3. を入力し、jquery (.load()または$.ajax()を入力してhrefページを呼び出します。
+0

は、preventDefaultを見てみましょう:https://developer.mozilla.org/en/DOM/event.preventDefault http://api.jquery.com/event。 preventDefault/ – n3on

+2

あなたは別のドメインから '$ .load'することはできません –

答えて

4

これはあなたが始める必要があります。

$(document).on('click', 'a', function(event) { 
    event.preventDefault(); // Now the link doesn't do anything 
    var href = this.href;  // The link's URL is in this variable 
}); 
+0

これはjavascript関数 – Ascherer

+0

から新しく作成されたタグのために働きません。 – Blender

+0

hahはちょうど鉱山を盗んだ – Ascherer

1

$(document).on('click', '*[href]', function(e) { 
    // Whatever you're trying to do 
    e.preventDefault(); 
    return false; 
}); 
+0

@Blender私はHTML6の "リンクとしてのタグ"について話していたと思いますが、今は見つからないようです... –

+0

あなたは任意のタグをクリックすることができます。スパン、またはボタン(それは何もしませんが、有効なhtmlではありませんが、それは許可されます) – Ascherer

0
$(document).on('click','a[href]',function(){ 
    var href = $(this).attr('href'); 

    $.ajax(href,function(data){ 
     $(your_container).html(data); 
     //data is the HTML returned 
     //note that ajax is bound to the 
     //same origin policy 
     //any request outside your domain won't work 
    }) 

    return false; 
}); 
0
<a href="javascript:void(0);">...</a> 

そしてあなたは、このアンカーのためのスクリプトを書くことができた後のような何かを行うことができます。

0

これはそれを行う必要があります。

$("a").click(function(event) { 
    event.preventDefault(); 
    var href = $(this).attr("href"); 
    $.ajax({ 
    url: href, 
    success: function(data) { 
     alert('Load was performed.'); 
    } 
    }); 
}); 
関連する問題