【问题标题】:How to target elements in another element using jQuery如何使用jQuery定位另一个元素中的元素
【发布时间】: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


【解决方案1】:

您必须考虑路径,例如文件夹等。

您在left 文件夹中有按钮,并且想要访问right 文件夹的按钮。因此,您必须从 left 文件夹转到父文件夹并从那里开始,如下所示:

$(this).parent().find(".right .b4")

这就像在做../right/b4

【讨论】:

  • 谢谢@JorgeFuentesGonzález。我会解决这个问题并回复。
  • 试过$(this).parent().find(".rightSide .b4").show();。它不起作用@JorgeFuentesGonzález
【解决方案2】:

试试这个。

我遍历了该元素的祖父母,我将在“.b4”类上执行查找并显示它

 $( '.b3' ).click(function() {
    console.log($(this).parentsUntil("tr").find(".b4"));
  $(this).parentsUntil("tr").find(".b4").show();
});

Js 小提琴 https://jsfiddle.net/xpvt214o/174861/

【讨论】:

  • 太奇怪了。它适用于 jsFiddle,但不适用于我的网页......将深入挖掘。
  • 我的错,也许你有更多的 dom 元素,但我想我可以看到其他人解决了你的问题。快乐编码
【解决方案3】:

你好,你可以试试这个方法

$(function() {
   $('table .b3').on('click', function(){
    
    // $(this).parent().nextUntil('rightSide').next().show();
    // $(this).parent().find(".rightSide").next().show();
    // $(this).next('td').next('td').find('.b4').show();
    $(this).parent().next('td').next('td').find('.b4, .b5').show();
   })
});	
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<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>
<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>
</table>

【讨论】:

  • 这并不能解决我需要做的事情,因为我的网页有很多 &lt;tr&gt; 的行——所以您的解决方案会在页面上显示所有 B4 和 B5 按钮。这就是为什么我认为我需要使用(this)
  • 嗨@H.Ferrence,我已经更新了答案,你期待什么解决方案
  • 优秀。谢谢@whoami ......很奇怪你的答案在我的脚本中有效,但(在)Kannan's 没有。不过他在 jsFiddle 中的作品。我选择 tis 作为答案只是因为它解决了我的问题。
猜你喜欢
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多