2012-04-13 85 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 2012-04-13 23:09:57

+2

您不能从另一个域名'.load' – 2012-04-13 23:11:10

回答

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 2012-04-13 23:10:56

+0

现在它应该。 – Blender 2012-04-13 23:11:52

+0

哈刚刚偷了我的 – Ascherer 2012-04-13 23:12:11

1

可以做类似

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

@Blender我相信有一些关于HTML6的“任何标签作为链接”的讨论,但我现在似乎无法找到它... – 2012-04-13 23:35:50

+0

你可以点击任何标签,这将允许你有一个href跨度或一个按钮(虽然它不会做任何事情,并且不是有效的html,它会允许它) – Ascherer 2012-04-13 23:59:17

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