2011-08-26 42 views
0

需要識別div中的每個'a'標籤並綁定onclick事件。我綁定了相同的標籤。我需要在點擊的'a'標籤下添加一些文本。但是當我嘗試我的代碼綁定到div中的所有'a'標籤。我可以如何指定我點擊過的'a'標籤。我可以使用jquery對div中的內容進行更改。可以修改'a ' 標籤。確定標籤

<div class="people_rt_link2"> 
    <a href="#" title="2011">2011</a><br><br> 
    <a href="#" title="2008">2008</a><br><br> 
    </div> 

    $(document).ready(function() { 
     var timesClicked = 0; 
     $('.people_rt_link2 a').bind('click', function() { 

     jQuery.post("<?php bloginfo('template_directory'); ?>/test.php", data, function(response) { 
       alert('Got this from the server: ' + response); 
       //this response should to binded to the clicked a tag 
       //$(this).after('<div>'+response+'</div>'); 
     }); 
     timesClicked++; 
     if (timesClicked >= 1) { 
      $(this).unbind(); 
     } 

    }); 
    }); 

回答

0

使用this指的點擊<a>。例如使用jQuery:

$('div.people_rt_link2 a').click(function(){ 
    $(this).unbind('click'); 
    var that = this; 
    $.post('somepage.php', function(data){ 
     $(that).after(data); 
    }); 
    return false; 
}); 

Example

讓我知道這是不是正是你想要的目的。

+0

paulPRO嗨,我有這樣的解決方案,它的工作原理fine.But我怎樣才能達到同樣的一個jQuery post.On的一個標籤內單擊ajax被執行,我需要從服務器獲取響應並將其附加到單擊的錨定標記。 – Gins

+0

@Gins,我更新了我的帖子,向'somepage.php'發出POST請求,並在ajax請求完成後插入數據。 – Paulpro

+0

嗨,我試過這個,但不工作。 – Gins

0

的Theres很多方法來指定要添加文字太標籤..第一個孩子..第一類的..但多數時候只是追加一個ID,標籤,讓JavaScript的處理吧..

$("people_rt_link2 a.YourClass").bind("click", function({do stuff})); 

http://www.w3schools.com/tags/tag_a.asp

0

嘗試這樣的事:

$("people_rt_link2 a").bind("click", foo(<!--Put here all your code you want to be executed -->));