jQuery:引用和操作动态添加元素的父元素

发布于 2025-01-02 12:41:27 字数 1387 浏览 3 评论 0原文

我正在文档中加载一些 XML 并向其中添加一个 DIV 按钮。通过单击按钮,我不仅不想 fadeOut() 按钮,而且还不想淡出其父元素。虽然引用和淡出按钮本身正在工作,但我无法访问其父元素。 (请参阅我的代码中的最后几行)。

$(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!!
}

更新:

XML/HTML 将如下所示:

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

我想隐藏整个part_gp..

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

也不起作用..

I'm loading some XML in my document and add a DIV-button to it. By clicking the button, I wan't to fadeOut() not only the button, but also its parent element. While referencing and fading out the button itself is working, I fail to get access to its parent-element. (see the last lines in my code).

$(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:

The XML/HTML will look like this:

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

I want to hide the whole part_gp..

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

didn't work either..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2025-01-09 12:41:27

问题是您正在使用 $(this).after()。这就是将按钮放在要隐藏的块之后,而不是在其内部。你真正想要的是$(this).append()

http://api.jquery.com/after/
http://api.jquery.com/append/

The problem is that you're using $(this).after(). That is putting your button after the block you want to hide, not inside of it. What you really want is $(this).append().

http://api.jquery.com/after/
http://api.jquery.com/append/

殊姿 2025-01-09 12:41:27

我不知道标记是什么样子,但你可以尝试

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

i dont know how the markup looks like but you can try

$(this).parents("div.someClassForIdentification").fadeOut();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文