【发布时间】:2018-10-02 00:58:05
【问题描述】:
我想在网页上的特定元素容器中定位某些<button>。
这是我的情况:
我正在尝试通过单击按钮“B3”来.show() 按钮的 B4 和 B5。
这是我的标记:
<tr>
<td class="leftSide">
<button class="b1" style="display:inline">B1</button>
<button class="b2" style="display:inline">B2</button>
<button class="b3" style="display:inline">B3</button>
</td>
<td>Some middle-ground content</td>
<td class="rightSide">
<button class="b4" style="display:none">B4</button>
<button class="b5" style="display:none">B5</button>
</td>
</tr>
这是我的 jQuery:
$( '.b3' ).click(function() {
$(this).hide();
$(this).siblings('.b4').show(); // tried this and it does not work
$(this).siblings('.b5').show(); // tried this and it does not work
$(this).nearest('.b4').show(); // tried this but it throws an error
$(this).nearest('.b5').show(); // tried this but it throws an error
$(this).closest('.b4').show(); // tried this and it does not work
$(this).closest('.b5').show(); // tried this and it does not work
$(this).next('.b4').show(); // tried this and it does not work
$(this).next('.b5').show(); // tried this and it does not work
$(this).parent().find(".rightSide .b4").show(); // tried this and it does not work
$(this).parent().find(".rightSide .b5").show(); // tried this and it does not work
});
是否可以访问右侧容器中的按钮?
【问题讨论】:
-
它们在不同的父级下,因此如果您尝试使用
$(this)获取这些元素,则必须先找到父级。 -
这有点奇怪。如果您解释您的情况,我们可以尝试找到更好的解决方案。
标签: jquery button next siblings closest