简体   繁体   中英

jQuery - find all first siblings of each node

HTML:

<ul class="treeview">
  <li>
    <a data-toggle="collapse" href=".options1"> <!-- this one -->
      Item 1
    </a>
    <ul class="collapse options1">
      <li>
        <a data-toggle="collapse" href=".sublevel1"></a> <!-- this one -->
        <input type="checkbox" value="option1" />
        <a data-toggle="collapse" href=".sublevel1">Item 1-1</a> <!-- not this one -->
        <ul class="collapse sublevel1">
          <li>
              Item 1-1-1
            <ul>
              <li><input type="checkbox" value="option1" />
                Item 1-1-1-1
              </li>
              <li><a data-toggle="collapse" href=".collapse1-1-1-1-1"></a> <!-- this one -->
              <input type="checkbox" value="option1" />
                <a data-toggle="collapse" href=".collapse1-1-1-1-1">Item 1-1-1-2</a> <!-- not this one -->
                <ul class="collapse collapse1-1-1-1-1">
                  <li>Item 1-1-1-2-1
                    <ul>
                      <li><input type="checkbox" value="option1" />Item 1-1-1-2-1-1</li>
                      <li><a data-toggle="collapse" href=".collapse1-1-1-1-1-1-1"></a><input type="checkbox" value="option1" />
                        <a data-toggle="collapse" href=".collapse1-1-1-1-1-1-1">Item 1-1-1-2-1-2</a>
                        <ul class="collapse collapse1-1-1-1-1-1-1">
                          <li>Item 1-1-1-2-1-2-1
                            <ul>
                              <li><input type="checkbox" value="option1" />Item 1-1-1-2-1-2-1-1</li>
                              <li><input type="checkbox" value="option1" />Item 1-1-1-2-1-2-1-2</li>
                              <li><input type="checkbox" value="option1" />Item 1-1-1-2-1-2-1-3</li>
                              <li><input type="checkbox" value="option1" />Item 1-1-1-2-1-2-1-4</li>
                            </ul>
                          </li>
                        </ul>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </li>
        </ul>
      </li>
      <li><input type="checkbox" value="option1" />Item 1-2</li>
      <li><input type="checkbox" value="option1" />Item 1-3</li>
      <li><input type="checkbox" value="option1" />Item 1-4</li>
    </ul>
  </li>
</ul>

jQuery:

$('.treeview ul').not('ul:not([class])').siblings('a')
        .prepend("<span>XYZ</span>");

As you can see, I'm already looking for all <ul> 's which don't have ANY classname, and then looking for <a> siblings, but I only need the first sibling (before the input checkbox).

Here's a jsFiddle to help you out: https://jsfiddle.net/st2yehgr/1/ (tip: all the item that have double 'XYZ', I only want the first one)

Can anyone help me here?? Cheers! :)

You can try this

$('.treeview li>a:first-child')
        .prepend("<span>XYZ</span>");

Check this fiddle

Answered by :@mplungjan

$('.treeview ul').not('ul:not([class])').each(function() {
 $(this).find("a").first().prepend("<span>XYZ</span>");
});

His fiddle: https://jsfiddle.net/qtrhu249/1/ (which I then updated the html to also cover the first level [wrapped everything in a div]).

$('.collapse li').find( "input[type='checkbox']" ).prev().prepend("<span>XYZ</span>")

尝试这个

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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