2

前のページを見つける方法がわかりません。クラス「cmdとパス」をjQueryで。

ここに私のhtmlコードがあります:

<tr>
    <td>MySQL</td>
    <td class="path">/etc/init.d/mysql</td>
    <td class="cmd">status</td>
    <td><i class="icon-remove"></i> <i class="icon-pencil"></i></td>
</tr>

私のjQueryコードは次のようになります。

$('i.icon-pencil').click(function() {
    var text = $(this).prev('.cmd').text();
    alert(text);
});
4

6 に答える 6

2
$('i.icon-pencil').click(function() {
    var text = $(this).closest('td').prev('.cmd').text();
    alert(text);
});

フィドル

于 2013-05-13T12:58:22.423 に答える
2
$('i.icon-pencil').click(function() {
    var tr = $(this).closest('tr'),
        txtPath = tr.find('.path').text(),
        txtCmd = tr.find('.cmd').text();

});
于 2013-05-13T13:00:34.787 に答える
1

以下を使用できます。

$('i.icon-pencil').click(function() {
    var parent = $(this).parent(),
        txtCmd = parent.prev('.cmd').text(),
        txtPath = parent.prev('.path').text();
});
于 2013-05-13T12:59:17.223 に答える
0
$('i.icon-pencil').click(function() {
    var text = $(this).closest('tr').children('.cmd').text(); // .path can also be the selector.
    alert(text);
});

別の方法は次を使用してい.prevAll()ます:

$('i.icon-pencil').click(function() {
    var text = $(this).parent().prevAll('.cmd').text(); // .path can also be the selector.
    alert(text);
});
于 2013-05-13T13:00:03.567 に答える