的父母,我有以下HTML:JQuery的:最后一个孩子封装嵌套元素
//'[popup]' hook nested at random depths, using <ul> or <ol>, no classes or id's
<ol>
<li>Blah</li>
<li>Blah</li>
<li>Foo
<ol>
<li>[popup]Bar</li>
</ol>
</li>
</ol>
我希望的目标[popup]
挂钩,并给予其<li>
类弹出,弹出/ N的。然后,我希望给它的父母(Foo)<li>
不同类型的箭头,arr/n。
下面是代码:
var num = 0;
//Find [popup] instances, increment the number
$("li:contains('[popup]') li:last-child").each(function() {
var nextnumber = num++;
//add a general and a unique class to the list item containing the hook
$(this).addClass('popup' + ' ' + 'pop' + nextnumber);
//Get the parent list item, and give it general and unique classes also.
$thisArrow = $(this).parent().parent();
$thisArrow.addClass('arrow' + ' ' + 'arr' + nextnumber);
});
即工作目标没有问题的嵌套深度只有一层,但不超过这一点。当与目标嵌套两层深列表呈现,会出现以下情况:
<ol>
<li>Blah</li>
<li class="popup pop1 arrow arr1">Blah
<ol>
<li class="arrow arr0">Foo
<ol>
<li class="popup pop0">[popup]Bar</li>
</ol>
</li>
<li class="arrow arr2">Foo
<ol>
<li class="popup pop2">[popup]Bar</li>
</ol>
</li>
</ol>
</li>
</ol>
你可以看到,当最后(总是在列表的最后一次出现):最后一子元素达到,不知怎的,其父母的父母被赋予目标和父母的类别,,然后类别被添加到它本身。
如果有人能指出我的错误,我会非常感激。
(我已经试过.parents().eq(1);
,而不是.parent().parent()
相同的结果。)
你可以尝试后你想要的结果不明白的问题,所以我们可以把它比作是你得到。我有点困惑,你想达到什么 - 创建一个JsFiddle与你的源http://jsfiddle.net/Jg9kv/ – 2012-04-12 12:35:35
@Marco道歉 - 实际使用案例在这里。 A部分是我想要的,D部分是当目标嵌套在一层以上时发生的情况。 http://databizsolutions.ie/contents/page.php?v=35&u=admin-videos#d – Eamonn 2012-04-12 12:43:47