简体   繁体   中英

Find first occurrence of list item above specific class - JQuery

So I have a list, and I'm trying to select the last occurrence of the 'nav-header' class in the list above the only list item with the class of 'active'.

<li class="nav-header"><a href="#" class="slide" id="general">Main</a></li>
<li class="general"><a href="index.php">Home</a></li>
<li class="general"><a href="faq.php">FAQ</a></li>
<li class="general"><a href="wiki.php">Wiki</a></li>
<li class="general"><a href="tos.php">TOS</a></li>
<li class="general"><a href="helpdesk.php">Help Desk</a></li>
<li class="general"><a href="development.php">Changelog</a></li>
<li class="general"><a href="tokens.php">Tokens</a></li>
<li class="general"><a href="stats.php">Stats</a></li>
<li class="nav-header"><a href="#" class="slide" id="community">Community</a></li>
<li class="community"><a href="notepad.php">Notepad</a></li>
<li class="community"><a href="messaging.php">Messaging</a></li>
<li class="community"><a href="forums/vforum.php?page=1">Main Forum</a></li>
<li class="community"><a href="cforum/vforum.php?page=1">Network Forum</a></li>
<li class="community"><a href="online.php">Currently Online</a></li>
<li class="community active"><a href="find.php">Search</a></li>
<li class="community"><a href="gm.php">Global Market</a></li>
<li class="community"><a href="bm.php">Black Market</a></li>
<li class="community"><a href="mostwanted.php">Most Wanted</a></li>

For example, when the 'Search' list item is active, I want to find and return ONLY the Community list item. I can get it to return both Community and Main via the .prevAll function, but can't for the life of me figure out how to return just one. Thanks for the help.

尝试:first选择器,请参阅演示

$('li.active').prevAll('.nav-header:first')

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