javascript - 访问没有 ID 的元素的父元素

标签 javascript jquery

如何获取值为 WAN 的 li 的父级无需使用 ID?我已经尝试过使用 $(this).parent().parent();onclick对于input但它只返回点击的 li .

<li>
   <span class="expanded">-</span>
   <input type="checkbox" name="Application Integration">Application Integration
   <ul class="">
      <li>
         <span class="expanded">-</span>
         <input type="checkbox" name="Windows">Windows
         <ul class="">
            <li>
               <span class="expanded">-</span>
               <input type="checkbox" name="Leitungen">Leitungen
               <ul class="">
                  <li><span>&nbsp;</span><input type="checkbox" name="WAN">WAN</li>
                  <li><span>&nbsp;</span><input type="checkbox" name="Mail">Mail</li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
</li>

我如何访问整个构造(从 <input type="checkbox" name="Application Integration">Application Integration 开始)?

非常感谢您的任何建议。

最佳答案

你想要这样的东西吗?

$('[name="Application Integration"]').on('click', function(){
   $(this).closest('li').find('[name="WAN"]').closest('li').addClass('xxx');
});
.xxx {
    border: solid 3px orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>
   <span class="expanded">-</span>
   <input type="checkbox" name="Application Integration">Application Integration
   <ul class="">
      <li>
         <span class="expanded">-</span>
         <input type="checkbox" name="Windows">Windows
         <ul class="">
            <li>
               <span class="expanded">-</span>
               <input type="checkbox" name="Leitungen">Leitungen
               <ul class="">
                  <li><span>&nbsp;</span><input type="checkbox" name="WAN">WAN</li>
                  <li><span>&nbsp;</span><input type="checkbox" name="Mail">Mail</li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
</li>

反之亦然

$('[type="checkbox"]').on('click', function(){
   $(this).parents('li').last().children('input').not($(this)).prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>
   <span class="expanded">-</span>
   <input type="checkbox" name="Application Integration">Application Integration
   <ul class="">
      <li>
         <span class="expanded">-</span>
         <input type="checkbox" name="Windows">Windows
         <ul class="">
            <li>
               <span class="expanded">-</span>
               <input type="checkbox" name="Leitungen">Leitungen
               <ul class="">
                  <li><span>&nbsp;</span><input type="checkbox" name="WAN">WAN</li>
                  <li><span>&nbsp;</span><input type="checkbox" name="Mail">Mail</li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
</li>

关于javascript - 访问没有 ID 的元素的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46475045/

相关文章:

javascript - 如何在另一个文件中使用 MongoDB 集合

javascript - 无法通过 AJAX 将 JSON 渲染为 HTML

javascript - 在 Angularjs 中突出显示选定的 anchor 标记(使用 ng-repeat)

javascript - 是否可以将诸如 Geolocation.watchPosition() 之类的函数包装在 Promise 中?

javascript - 在 Angular 中有条件地添加 css 问题?

javascript - 将每一个结果一一展示

javascript - LocalStorage 在 chrome 的不同选项卡中返回 null

jquery - 如何将 jqgrid 列与列标题对齐?

jquery - 更改嵌套行类 onclick

javascript - jQuery 如何使用延迟加载在滚动条上加载更多内容