2012-02-02 75 views
0

我在文档中加载了一些XML,并为其添加了DIV按钮。通过点击按钮,我不仅要按下按钮,还要按其父元素。在引用和淡出按钮本身工作时,我无法访问其父元素。 (请参阅我的代码中的最后几行)。jQuery:引用和操纵动态添加的元素的父元素

$(document).ready(function(){ 

    var searchstring = decodeURIComponent(getUrlVars()["search"]); 

    $.ajax({ 
     url: 'data.xml', 
     type: 'GET', 
     dataType: 'xml', 
     timeout: 10000, 
     error: function(){ 
      alert('Error loading XML document'); 
     }, 
     success: function(xml){ 
      $(xml).find('entry').filter(function(index) { return $('lemma', this).text() == searchstring; }).each(function() { 
       $(this).find('part_gp > part').each(function(){ 
        var more = $('<div class="more">+</div>'); 
        $(this).after(more); 
        more.click(fader); 
       }); 

      $("#result").append($(this)); 

      }); 
      } 
    }); 
}); 

function fader() { 
    $(this).fadeOut(); // this works!! 
    $(this).parent().fadeOut(); // this doesn't work!! 
} 

UPDATE:

的XML/HTML看起来像这样:

<part_gp> 
    <part>Grafschaft</part> 
    <div class="more">+</div> 
    <pos> feminin/weiblich</pos> 
</part_gp> 

我想隐藏整个part_gp ..

$(this).parents("part_gp").fadeOut(); 

也不能工作..

回答

0

我不知道ho W上的标记类似,但你可以尝试

$(this).parents("div.someClassForIdentification").fadeOut();