jQuery parents()和parent()。parent()只看到一个级别?

时间:2009-02-18 23:42:52

标签: javascript jquery dom

我在使用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"]
               +"'>&nbsp;&nbsp;"
               +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 个答案:

答案 0 :(得分:2)

没关系......我试图在删除它后访问它。只需要重新安排我的任务。