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

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

I have the following:

  • item1
    • sub1
    • sub2
7条回答
  •  旧巷少年郎
    2020-12-10 13:04

    What about this approach? Clone the object, remove the clone's children and then find the text content.

    $("#list li").click(function () {
        alert($(this).clone().children().remove().end().text());
    }
    

    Perhaps not the most efficient approach, but it's entirely intuitive, pretty tidy, and you don't have to muck with your html.

提交回复
热议问题