下面是我的代码,我的目标是当用户点击<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());
});
答案 0 :(得分: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
答案 1 :(得分:0)
您可以尝试:
$('a').click(function(){
alert($(this).parent().parent().find('.par').html());
});
答案 2 :(得分:0)
$(this).parent().next().next().find('.par').html()
答案 3 :(得分:0)
试试这个:
$('a').click(function(){
alert($(this).parents('table').find('.par').html());
});
答案 4 :(得分:0)
如果您要提醒的内容位于类(或ID)定义的标记中,您可以这样做:
$('a').click(function(){
alert($('.par').html());
});
如果没有并且它在最后一个兄弟中,无论是否定义,代码都是
$('a').click(function(){
alert($(this).parents().get(0).siblings(':last').text());
});
答案 5 :(得分:0)
您可以尝试使用较短的一个:
$('a').click(function(){
alert($(this).closest('tr').find('.par').html());
});
答案 6 :(得分:0)
$('a').click(function(){
alert($('.par').text()); // Simple way
alert($(this).parent().parent().find('.par').html()); // Find through parent
});