简体   繁体   中英

how to get all 'li' element from #1 to #demo1 using jquery

this is my code:

<div id="demo1">
    <ul>
        <li>aaa</li>
        <li>
            <ul>
                <li>hhh</li>
                <li>qqqq</li>
            </ul>
        </li>
        <li>qqqqq</li>
        <li>
            <li>lll</li>
            <li>
                <ul>
                    <li>qqwed</li>
                    <li id="1">qqdwdw</li>
                </ul>
            </li>
        </li>
    </ul>
</div>

i have many 'ul' and 'li' elements , now i can get the li '#1',and the div '#demo1'

i want to get all 'li' elements between them ,

how to get it,

i use this code , like next code , but not successful,

$('#1').parent('li')

$('#1').closest('li') 

but , how to make it between the '#demo1' too ?

thanks

You can use .parentsUntil() to get the parents, then .filter() to get on the <li > elements, like this:

$("#1").parentsUntil("#demo1").filter('li');

I know this is an example, but IDs (in HTML4 at least) can't start with a number either.

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