如何在不影响同一类的其他表的情况下添加行?

时间:2013-06-20 04:43:01

标签: jquery html-table

这是我的脚本使用追加:

$("a.addrow").click(function(){
        $(".itemlist").append('<tr><td>Content</td></tr>');
    });

我有这个HTML

<table class="itemlist">
    <tr>
        <td>Content</td>
    </tr>
</table>
<a href="#" class="addrow">Add Row</a>

<table class="itemlist">
    <tr>
        <td>Content</td>
    </tr>
</table>
<a href="#" class="addrow">Add Row</a>

现在问题是,这会向两个表而不是相关表添加行。我怎么做?而且我如何防止锚跳跃,因为我正在使用#for href?

非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

$("a.addrow").click(function(event){
    //Get the first match and append the data, if you want another result, you could use .eq().
    //So .eq(0) === .first()
    $(".itemlist").first().append('<tr><td>Content</td></tr>');

    //Prevent the anchor 'jumping'
    event.preventDefault();
});

定位特定表的另一种方法是添加一个独特的类或ID,但我想这不是一个选项。

相关问题