简体   繁体   English

如何选择所有列表项,但列表项没有嵌套子项 <ul> 要么 <ol>

[英]How to select all list items but list items don't have nested child of <ul> or <ol>

I want to select all list items in child of " .favdelParentFold " but make sure list items not have any nested child of <ul> . 我想在“ .favdelParentFold ”的子项中选择所有列表项,但要确保列表项没有<ul>任何嵌套子项。 If list items have any nested child " <ul> " I don't want to trigger it. 如果列表项中有任何嵌套子级“ <ul> ”,我不想触发它。

HTML HTML

<li class="favdelParentFold">
    <a href=""><span class="caret-right"></span></a>
    <span class="folder"></span><span>fold2</span><span class="trashcan">press me</span>

    <ul>
        <li><a href="">One 1</a><span class="trashcan">press me</span></li>
        <li><a href="">Two 2</a><span class="trashcan">press me</span></li>
        <li><a href="">Three 3</a><span class="trashcan">press me</span></li>
        <li>
            <a href="">Four 4</a><span class="trashcan">press me</span>
            <ul>
                <li>four 1.1</li>
                <li>four 1.2</li>
            </ul>
        </li>
    </ul>
</li>

JS file: JS档案:

$(document).ready(function () {
    $(".favdelParentFold ~ ul li.trashcan").click(function () {
        var tr = $(this).prev('span').text();
        var pa = $(this).closet().text();
        alert(tr);
        alert(pa);
    });
});

EX: EX:

If I click on one 1, two 2, three 3 I can get the this value and parent value but four 4 have child of <ul> I want to avoid triggers. 如果单击1、2、2、3、3,我可以得到此值和父值,但是四个4的子级为<ul>我想避免触发。

Expected o/p: 预期输出:

If click "one 1" i need to get one 1 and fold2 如果单击“一个1”,我需要得到一个1和fold2

If click "one 2" i need to get one 2 and fold2 如果单击“一个2”,我需要得到一个2和fold2

If click "one 3" i need to get one 3 and fold2 如果单击“一个3”,我需要得到一个3和fold2

But we don't trigger "four 4" 但是我们不会触发“四个4”

I think you are looking for something like following. 我认为您正在寻找类似的东西。

 $(".favdelParentFold ul li .ecm-trashcan").click(function() { var tr = $(this).prev().text(); var ma = $(this).closest('.favdelSubFold').find('#sfDel').text(); var pa = $(this).closest('.favdelParentFold').find('#pfDel').text(); alert(tr); if(tr!=ma) alert(ma); alert(pa); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li class="favdelParentFold"> <a href=""><span class="caret-right"></span></a> <span class="folder"></span><span>fold2</span><span class="trashcan">press me</span> <ul> <li><a href="">One 1</a><span class="trashcan">press me</span></li> <li><a href="">Two 2</a><span class="trashcan">press me</span></li> <li><a href="">Three 3</a><span class="trashcan">press me</span></li> <li> <a href="">Four 4</a><span class="trashcan">press me</span> <ul> <li>four 1.1</li> <li>four 1.2</li> </ul> </li> </ul> </li> 

Update: according to fiddle html the js will be like this: 更新:根据小提琴html, js将如下所示:

$(".favdelParentFold ul li .ecm-trashcan").click(function () {
    if ($(this).parent().find('ul').length || $(this).closest('.favdelSubFold').length) return;

    var tr = $(this).prev('a').text();
    var pa = $(this).closest('.favdelParentFold').find('#pfDel').text();
    alert(tr);
    alert(pa);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM