2011-04-01 67 views
1

我想通過jQuery點擊事件來替換我的hrefs鏈接:因爲我已經爲每個關注點放置了一個insite類。JQuery:綁定點擊hrefs

這是代碼我已經把我的頁面的底部:

$('.insite').each(function(i,a) { 
    $('this').click(function() { 
     $('#content').load(a.attr('href')+' #content'); 
    }); 
}); 

我是不是忘了什麼?

謝謝

+1

應該是$(本)而不是$('this')。 – Rezler 2011-04-01 13:02:44

回答

0

您是否包含jQuery庫?此外,您的代碼是否包含在DOM準備好的事件中?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script> 

$(function() { 
    $('.insite').click(function(e) { 
    $('#content').load($(this).attr('href')+' #content'); 
    }); 
}); 
0
$('.insite').click(function(){ 
    $('#content').load($(this).attr('href') + ' #content'); 
}); 
4
$('.insite').click(function(ev) { 
    $('#content').load($(this).attr('href')+' #content'); 
    ev.preventDefault(); 
}); 

你不需要每次。 而ev.preventDefault是需要的,因此它不會在瀏覽器中加載href。

+0

非常感謝! – Roch 2011-04-01 13:00:39

0

你不需要每個。你可以這樣做:

$('.insite').click(function(e) { 
    e.preventDefault(); 
    // Do stuff 
}); 

不知道你正在嘗試與$('#content').load(a.attr('href')+' #content');

0

做你應該使用

$(this) 

,而不是

$('this')