Jquery 中如果等于则删除元素

发布于 2024-12-19 03:34:44 字数 873 浏览 4 评论 0原文

如果内容等于 jquery 中设置的变量,我试图删除 jquery 中的元素。在此示例中,我尝试删除 .sub3,因为其内容等于变量集。

<div class="sub1">
    <a href="/large.jpg" class="group1 cboxElement">
    <img border="0" alt="" src="/thumb.jpg">
    </a>
</div>

<div class="sub2">
    <a href="/029-large.jpg" class="group1 cboxElement">
    <img border="0" alt="" src="/029-thumb.jpg">
    </a>
</div>

<div class="sub3">
    <a href="" class="group1 cboxElement"></a>
</div>

我想出的 Jquery 只是删除整个元素,无论

var nopic = <a class="group1 cboxElement" href=""></a>;

if($.trim($(".sub1").text()) == nopic) {
$(".sub1").remove();
}

if($.trim($(".sub2").text()) == nopic) {
$(".sub2").remove();
}

if($.trim($(".sub3").text()) == nopic) {
$(".sub3").remove();
}

感谢您的任何指示!

I am trying to remove an element in jquery if the contents are equal to a variable set in jquery. In this example I am trying to remove .sub3 as the contents are equal to the variable set.

<div class="sub1">
    <a href="/large.jpg" class="group1 cboxElement">
    <img border="0" alt="" src="/thumb.jpg">
    </a>
</div>

<div class="sub2">
    <a href="/029-large.jpg" class="group1 cboxElement">
    <img border="0" alt="" src="/029-thumb.jpg">
    </a>
</div>

<div class="sub3">
    <a href="" class="group1 cboxElement"></a>
</div>

The Jquery I have come up with just removes the whole element regardless

var nopic = <a class="group1 cboxElement" href=""></a>;

if($.trim($(".sub1").text()) == nopic) {
$(".sub1").remove();
}

if($.trim($(".sub2").text()) == nopic) {
$(".sub2").remove();
}

if($.trim($(".sub3").text()) == nopic) {
$(".sub3").remove();
}

Thanks for any pointers!

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

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

发布评论

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

评论(6

流年已逝 2024-12-26 03:34:44

适用于所有空链接的一行简单案例:

$('div[class^="sub"] a.group1.cboxElement[href=""]').parent('div').remove( )

One line, simple case that works on all empty links:

$('div[class^="sub"] a.group1.cboxElement[href=""]').parent('div').remove()

日久见人心 2024-12-26 03:34:44

如果您的目的只是删除不包含图像的内部 div,则以下内容就足够了:

$('div[class^="sub"]').filter(':not(:has(img))').remove();

If your intent is to just remove the inner div that does not contain an image the following will suffice:

$('div[class^="sub"]').filter(':not(:has(img))').remove();
泼猴你往哪里跑 2024-12-26 03:34:44

你应该使用 html()

var nopic = '<a class="group1 cboxElement" href=""></a>';

if($.trim($(".sub3").html()) == nopic) {
    $(".sub3").remove();
}

You should use html()

var nopic = '<a class="group1 cboxElement" href=""></a>';

if($.trim($(".sub3").html()) == nopic) {
    $(".sub3").remove();
}
饮湿 2024-12-26 03:34:44

.text() 返回节点的文本内容,而不是其 HTML。您需要 .html() 来代替。

例如

Hello

$('#blah').text() -> Hello
$('#blah').html() -> <a href="somewhere">Hello</a>

.text() returns the text content of a node, not its HTML. You want .html() instead.

e.g.

Hello

$('#blah').text() -> Hello
$('#blah').html() -> <a href="somewhere">Hello</a>
晨曦÷微暖 2024-12-26 03:34:44

我发现您遇到了三个问题:

  1. nopic 变量的赋值存在语法错误。您需要为分配给它的值加上引号。
  2. 鉴于您当前的 html,您的 .subX 元素内部没有任何文本。他们有html。因此,您应该使用 .html() 方法而不是 .text()
  3. 即使您解决了我在 #1 中描述的问题,它仍然无法工作,因为 nopic 的值不等于 .sub3 标记。 code> (我认为这是您用来测试的内容)。这是因为 hrefclass 属性颠倒了。

这是工作的 JS,以及一个实例

var nopic = '<a href="" class="group1 cboxElement"></a>';

if($.trim($(".sub1").html()) == nopic) {
        $(".sub1").remove();
}

if($.trim($(".sub2").html()) == nopic) {
    $(".sub2").remove();
}

if($.trim($(".sub3").html()) == nopic) {
    $(".sub3").remove();
}

You've got three problems I can see:

  1. syntax errors for your assignment of the nopic variable. You need quotes around the value you're assigning to it.
  2. Given your current html, your .subX elements don't have any text inside them. They have html. So you should be using the .html() method rather than .text().
  3. Even once you fix the issue I described in #1, it still won't work because the value of nopic won't be equal to the <a> tag in .sub3 (which I assume is what you're using to test). That's because the href and class attributes are reversed.

Here's the working JS, as well as a live example:

var nopic = '<a href="" class="group1 cboxElement"></a>';

if($.trim($(".sub1").html()) == nopic) {
        $(".sub1").remove();
}

if($.trim($(".sub2").html()) == nopic) {
    $(".sub2").remove();
}

if($.trim($(".sub3").html()) == nopic) {
    $(".sub3").remove();
}
橪书 2024-12-26 03:34:44

我认为你可能会倒退。您想要做的是删除包含该链接标记的 div 。找到您要查找的 a 并删除它的父级可能会更容易。

$('a.group1.cboxElement').parents('div[class^="sub"]').remove();

I think you may be going about this backwards. What you are trying to do is remove a div if it contains that link tag. It would probably be easier to find the a you're looking for and remove it's parent.

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