简体   繁体   English

.parentsUntil($(e.target).parents('ul')。length === 1)

[英].parentsUntil( $(e.target).parents('ul').length === 1 )

I've got a standard list with multiple levels and I need to check the root of the particular item until 我有一个包含多个级别的标准列表,我需要检查特定项目的根目录,直到

$(e.target).parents('ul').length === 1

Is it possible to use .parentsUntil() for this? 是否可以使用.parentsUntil() Or is there another method I can use so I can check if the root of the list item has a particular class or not? 还是我可以使用另一种方法来检查列表项的根是否具有特定的类?

What you are looking for is called .closest() . 您正在寻找什么叫做.closest()

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree . 描述: 对于集合中的每个元素,通过测试元素本身并遍历DOM树中的祖先,获得与选择器匹配的第一个元素

Example of use: 使用示例:

if( $(e.target).closest('ul').hasClass('cls') ) {
     //do something
     alert('Class Found');
}

You can use parentsUntil 您可以使用父母直到

$(e.target).parentsUntil('ul.someClass')

So it will select the ul with a class someClass 因此它将选择带有someClass类的ul

.length is use to check if it exist or not. .length用于检查它是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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