0

下面是我的代码,我的目标是当用户单击<a>我想提醒 class='sibling_3' 上的内容时,即'Hello'。

<td class="sibling_1">
    <a href="#">Click Me</a>
</td>
<td class="sibling_2"></td>
<td class="sibling_3">
    <p class="par">Hello</p>
</td>

我试过做下面的代码,但它给了我一个未定义的错误。

$('a').click(function(){
    alert($(this).closest('.par').html());
});
4

7 回答 7

0

如果您要提醒的内容在类(或 id)定义的标签中,您可以简单地执行以下操作:

$('a').click(function(){
    alert($('.par').html());
});

如果不是并且它在最后一个兄弟中,无论是否定义,代码将是

$('a').click(function(){
    alert($(this).parents().get(0).siblings(':last').text());
});
于 2013-05-17T06:22:09.243 回答
0

您可以尝试使用较短的:

$('a').click(function(){
    alert($(this).closest('tr').find('.par').html());
});

在此处查找演示

于 2013-05-17T06:23:29.723 回答
0
$('a').click(function () {
    alert($(this).closest('td').siblings().find('.par').html());
});

解释:

$(this)          // Get the current link clicked
.closest('td')   // Go to the closest parent td
.siblings()      // Get the siblings of td
.find('.par')    // find the element with .par class
.html()          // Get its content
于 2013-05-17T06:16:19.803 回答
0

你可以试试:

$('a').click(function(){
    alert($(this).parent().parent().find('.par').html());
});
于 2013-05-17T06:16:37.647 回答
0
$(this).parent().next().next().find('.par').html()
于 2013-05-17T06:16:58.310 回答
0

尝试这个:

$('a').click(function(){
    alert($(this).parents('table').find('.par').html());
});
于 2013-05-17T06:18:19.263 回答
0
$('a').click(function(){
    alert($('.par').text()); // Simple way 
    alert($(this).parent().parent().find('.par').html()); // Find through parent
});

演示

于 2013-05-17T06:25:06.287 回答