在给定类型的元素上方选择给定类型的最接近元素

时间:2018-10-10 16:19:21

标签: jquery

如何选择给定类型的元素中最接近给定类型的元素?

例如...

this当前为$('#j1_95_anchor'),我希望选择ID为<li>的{​​{1}}。请注意,我目前没有此ID值。

15.0

以下方法将起作用,但我认为可能有更直接的方法:

<ul role="group" class="jstree-children" style="">
    <li role="treeitem" aria-selected="false" aria-level="3"
        aria-labelledby="15.0_anchor" aria-expanded="true" id="15.0" class="jstree-node jstree-open" aria-busy="false"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="15.0_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Volts_AB</a>
        <ul role="group" class="jstree-children" style="">
            <li role="treeitem" aria-selected="false" aria-level="4" aria-labelledby="j1_90_anchor" id="j1_90" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="j1_90_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>ID: 15</a></li>
            <li role="treeitem" aria-selected="false"
                aria-level="4" aria-labelledby="j1_91_anchor" id="j1_91" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="j1_91_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Name: Volts_AB</a></li>
            <li role="treeitem" aria-selected="false"
                aria-level="4" aria-labelledby="j1_92_anchor" id="j1_92" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="j1_92_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Description: Voltage Phase A-B</a></li>
            <li role="treeitem"
                aria-selected="false" aria-level="4" aria-labelledby="j1_93_anchor" id="j1_93" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="j1_93_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Units: volts</a></li>
            <li role="treeitem" aria-selected="false"
                aria-level="4" aria-labelledby="j1_94_anchor" id="j1_94" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor" href="#" tabindex="-1" id="j1_94_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Type: Analog Input</a></li>
            <li role="treeitem" aria-selected="true"
                aria-level="4" aria-labelledby="j1_95_anchor" id="j1_95" class="jstree-node  jstree-leaf jstree-last">
                    <i class="jstree-icon jstree-ocl" role="presentation"></i>
                    <a class="jstree-anchor addPoint editable editable-click jstree-clicked" href="#" id="j1_95_anchor" data-original-title="" title=""><i class="jstree-icon jstree-themeicon glyphicon glyphicon-plus-sign jstree-themeicon-custom" role="presentation"></i>Add Point</a>
            </li>
        </ul>
    </li>
</ul>

我当时想可能是这样的:

$(this).closest('ul> li');

2 个答案:

答案 0 :(得分:2)

目标li#15.0包含元素的父级ul,因此:

$(this).parents('li').last()

严格回答您的问题“给定类型的元素上方的给定类型的元素”,您可以使用:

$(this).closest('li:has(ul)')

适用普通选择器。

答案 1 :(得分:0)

如果我理解正确,您也可以尝试以下方法:

 $(this).closest("[id='15.0']");

您也可以使用此功能:

$(this).closest('ul.jstree-children').children("li");

如果您喜欢棘手的方式,请查看以下代码:

$("this").closest("li:only-child")