0

我想在 JQuery 的帮助下编写一个简单的函数。我创建了这个 jfiddle:

<table class="my_table">
    <tr>
        <td class="correct"></td>
        <td>
            <input type="button" class="my_button" />
        </td>
    </tr>
    <tr>
        <td class="incorrect"></td>
        <td>
            <input type="button" class="my_button" />
        </td>
    </tr>
...

http://jsfiddle.net/Xz3xc/1/

点击后,在按钮上方添加了一些内容。我的本地机器就是这种情况。我在我的 wordpress 上运行另一个主题,那里的代码如下所示:

<table class="my_table">
    <tr>
        <td class="correct"></td>
        <td>
            <div>
            <input type="button" class="my_button" />
            </div>
        </td>
    </tr>
        <tr>
        <td class="correct"></td>
        <td>
            <div>
            <input type="button" class="my_button" />
            </div>
        </td>
    </tr>
...

http://jsfiddle.net/KuACZ/1/

在那里,当然代码不起作用,因为函数中缺少一个 parent()。我如何编写这个通用函数,它总是在单击按钮的同一个 tr 中获取第一个 td,以便它独立于主题。

谢谢!

4

1 回答 1

2

这段代码在同一个中找到所有的td's with class 。correcttr

$('.my_button').click(function() {
   $(this).parentsUntil("tr").siblings("td.correct").html('Test'); 
});

jsFiddle

于 2013-02-26T22:14:17.957 回答