2011-08-26 43 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 -->));