提问人:Eli 提问时间:2/19/2009 最后编辑:Shog9Eli 更新时间:12/12/2010 访问量:7832
jQuery parents() 和 parent().parent() 只看到一级?
jQuery parents() and parent().parent() only see one level up?
问:
我在使用jQuery获取元素的父级列表时遇到了一些问题。
也许我遗漏了一些东西,但我在几个嵌套的 div 中有一个链接,并且正在尝试获取父级父级的兄弟姐妹列表。
不过,我似乎只能从链接上一级,所以我不能
HTML 如下所示:
<div class="alertcontainer">
<div id="alertnode_1" class="alertnode">
<div class="alertnodeheader">Escalate to Level 1</div>
<div class="alertnodebody">
<div class="bold">Alert these people:</div><br/>
<div class="alertnodebodycontactlist">
<div class="alertnodebodycontact">187
<a id="d0.9659762698410487"
onclick="RemoveNode(this, 187)"
href="#">Remove Me...</a>
</div>
<div class="alertnodebodycontact">185
<a id="d0.6609632486132389"
onclick="RemoveNode(this, 185)"
href="#">Remove Me...</a>
</div>
<div class="alertnodebodycontact">184
<a id="d0.13180038199138278"
onclick="RemoveNode(this, 184)"
href="#">Remove Me...</a>
</div>
<div class="alertnodebodycontact">186
<a id="d0.6364304467227213"
onclick="RemoveNode(this, 186)"
href="#">Remove Me...</a>
</div>
<select class="esccontactlist" id="esc_contact_list_1">
<option>Also alert...</option>
</select>
</div>
<br/>
</div>
</div>
</div>
JS/jQuery如下:
function RemoveNode(e, id)
{
// Remove the contact
$(e).parents().filter('.alertnodebodycontact').remove();
// Add it back into the list.
for ( var i = 0; i < ContactList.length; i++)
{
if ( id == ContactList[i]["ContactID"] )
{
$('#esc_contact_list_1')
.append("<option value='"
+ContactList[i]["ContactID"]
+"'> "
+ContactList[i]["ContactName"]
+" ("
+ContactList[i]["PrimaryEmail"]
+")</option>");
}
// Here, I want to count the number of '.alertnodebodycontact' divs.
// However, $(e).parents() only returns a single item, which is the
// '.alertnodebodycontact' that the link is in.
// Likewise, $(e).parent().parent() returns nothing.
// Anyone have any idea why the outer divs aren't being returned?
}
}
答:
2赞
Eli
2/19/2009
#1
没关系。。。删除它后,我正在尝试访问它。只需要重新排序我的任务。
评论
1赞
Brent Dillingham
1/11/2010
谢谢你 - 让我意识到我在做同样的事情。用 trigger() 调用的事件没有冒泡,这让我意识到这是因为我在从 DOM 中删除该元素后从该元素中触发了该事件:)
1赞
Eli
1/12/2010
很高兴知道我不是唯一一个...... =o)
评论