jQuery 选择 div 的父级

发布于 2024-09-19 12:34:55 字数 888 浏览 2 评论 0原文

我有这个html:

    <div class="vt ddsitem">
        <a href="url"><img class="pt" id="xyz1" src="url"></a>
        <div>
            <img class="updown" src="images/updown.gif">
            <a href="url"><img class="bin" src="images/bin.gif"></a>
        </div>
    </div>
   <div class="vt ddsitem">
        <a href="url"><img class="pt" id="xyz2" src="url"></a>
        <div>
            <img class="updown" src="images/updown.gif">
            <a href="url"><img class="bin" src="images/bin.gif"></a>
        </div>
    </div>

并且我想删除vt ddsitem div,其中图像中xyz2是一个子项。

尝试了很多事情,例如:

$('#xyz2').parent().parent().remove(); 

但没有一个成功。

有人有线索吗?

I have this html:

    <div class="vt ddsitem">
        <a href="url"><img class="pt" id="xyz1" src="url"></a>
        <div>
            <img class="updown" src="images/updown.gif">
            <a href="url"><img class="bin" src="images/bin.gif"></a>
        </div>
    </div>
   <div class="vt ddsitem">
        <a href="url"><img class="pt" id="xyz2" src="url"></a>
        <div>
            <img class="updown" src="images/updown.gif">
            <a href="url"><img class="bin" src="images/bin.gif"></a>
        </div>
    </div>

And i want to remove the vt ddsitem div in which in the image xyz2 is a child.

Tried a lot of things, like:

$('#xyz2').parent().parent().remove(); 

but none of them did the trick.

Anyone got a clue?

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

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

发布评论

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

评论(2

暮年 2024-09-26 12:34:55

.closest() 是执行任务的好方法之后:

$('#xyz2').closest('.ddsitem').remove(); 

不过,如果您的内容在 document.ready 处理程序中运行,那么您所拥有的应该可以工作,如下所示:

$(function() {
  $('#xyz2').parent().parent().remove(); 
});

您可以在此处查看它的实际效果,相同的 document.ready 包装器适用于 .closest() 上面的方法...元素需要准备好并在 DOM 中,然后我们才能找到它们与选择器。

.closest() is a good method to do what you're after:

$('#xyz2').closest('.ddsitem').remove(); 

Though, what you have should work, if it's running in a document.ready handler, like this:

$(function() {
  $('#xyz2').parent().parent().remove(); 
});

You can see it in action here, the same document.ready wrapper goes for the .closest() method above...the elements need to be ready and in the DOM before we can find them with a selector.

傲影 2024-09-26 12:34:55

你的代码看起来没问题。

你使用任何 jQuery 插件吗?如果这样做,它一定是包装了您的 div 或转换了您的代码。尝试使用 Firebug 查看发生的情况。

Your code seems to be ok.

Are you using any jQuery plugin? If you do, it must be wrapping your div or transforming your code. Try to use Firebug to view what happening.

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