Javascript jQuery parents()和parent()。parent()只能看到上一级吗?
我在使用jQuery获取元素的父元素列表时遇到一些问题 也许我遗漏了一些东西,但我在几个嵌套的div中有一个链接,我正在尝试获取父级的父级的兄弟姐妹列表 不过,我似乎只能从链接升级一级,所以我不能 HTML格式如下:Javascript jQuery parents()和parent()。parent()只能看到上一级吗?,javascript,jquery,dom,Javascript,Jquery,Dom,我在使用jQuery获取元素的父元素列表时遇到一些问题 也许我遗漏了一些东西,但我在几个嵌套的div中有一个链接,我正在尝试获取父级的父级的兄弟姐妹列表 不过,我似乎只能从链接升级一级,所以我不能 HTML格式如下: <div class="alertcontainer"> <div id="alertnode_1" class="alertnode"> <div class="alertnodeheader">Escalate to Lev
<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>
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?
}
}
升级到1级
提醒这些人:
187
185
184
186
也提醒。。。
JS/jQuery如下:
<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>
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?
}
}
函数RemoveNode(e,id)
{
//拆下触点
$(e).parents().filter('.alertnodebodycontact').remove();
//将其重新添加到列表中。
对于(变量i=0;i
无需担心。。。我正在尝试在删除它后访问它。只需要重新安排我的任务。您可以绑定到$(document).ready()中每个锚点的单击事件,并将其本身作为参数传递,以及它的父文本。感谢您的帮助-让我意识到我正在做同样的事情。使用trigger()调用的事件没有冒泡,这让我意识到这是因为在从DOM中删除元素后,我从元素中触发了事件:)很高兴知道我不是唯一一个=o)