jQuery: Find the text of a list item that contains a nested ul

后端 未结 7 1603
暖寄归人
暖寄归人 2020-12-10 12:49

I have the following:

  • item1
    • sub1
    • sub2
相关标签:
7条回答
  • 2020-12-10 13:11

    I think you need to use the each method. so try the following script:

    $(document).ready(function() {
            $('ul').each(function() {
                $(this).find('li').click(function() {
                    var listItem = this;
                    alert($(listItem).text());
                });
            })
        });
    

    with the following markup

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题