简体   繁体   English

jQuery-查找LI的UL

[英]jQuery - Find UL of LI

Hey guys I was just wondering whether there is a way to find out what Unordered List the List Item is in? 大家好,我只是想知道是否有办法找出列表项所在的无序列表? So like for example I have a UL with the ID 1 and another one with the ID 2 and I'd have the List Items under both Unordered List sortable but on update I want to find out what Unordered List the List Item has been left in. 因此,例如,我有一个ID为1的UL,另一个ID为2的UL,并且我在“无序列表”下都可以对列表项进行排序,但是在更新时,我想找出列表项留在了什么“无序列表”中。 。

How Would I do this? 我该怎么做?

您可以为此使用parents()方法

$("#ID1").parents("ul");

just find its parent ul using parent() 只需its parent ul using parent()找到its parent ul using

$("li").parents("ul");

Demo Fiddle 演示小提琴

Your best bet would be to use closest rather than parents in case you have nested lists. 最好的选择是在嵌套列表的情况下使用closest而不是parents closest will only return the nearest ancestor matching the selector as opposed to parents which will return all ancestors matching (potentially multiple instead of just one). closest将仅返回与选择器匹配的最接近的祖先,而与之相反的parents将返回所有匹配的祖先(可能是多个而不是一个)。

Assuming in your case this is your li : 你的情况。假设this是你li

$(this).closest("ul");

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

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