具有相同类的多个链接

时间:2013-02-17 14:46:34

标签: javascript hyperlink jquery

我的页面上有多个使用相同类的链接:

<a class="tweetAnswer" rel="link1" href="">Answer</a>   

当有人点击其中一个链接时,我正试图用jQuery显示警告。不幸的是它在Firefox上没有任何显示......

$('a.tweetAnswer').live("click", function(){
    event.preventDefault();
    alert($(this).attr('rel'));
});

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

live自1.7以来已弃用,已在1.9版中删除。尝试:

$('a.tweetAnswer').on("click", function(event){
    event.preventDefault();
    alert($(this).attr('rel'));
});

编辑:adeneo很好地捕捉到缺少事件参数。

这是一个有效的jsfiddle

答案 1 :(得分:0)

首先,没有事件参数:

$('a.tweetAnswer').live("click", function(event){
    event.preventDefault();
    alert( $(this).attr('rel') );
});

其次,你应该在jQuery 1.7或更新版本中使用on():

$(document).on('click', 'a.tweetAnswer', function(event){
    event.preventDefault();
    alert( $(this).attr('rel') );
});