Jquery 中如果等于则删除元素
如果内容等于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
适用于所有空链接的一行简单案例:
$('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()
如果您的目的只是删除不包含图像的内部 div,则以下内容就足够了:
If your intent is to just remove the inner div that does not contain an image the following will suffice:
你应该使用 html()
You should use html()
.text()
返回节点的文本内容,而不是其 HTML。您需要.html()
来代替。例如
Hello
.text()
returns the text content of a node, not its HTML. You want.html()
instead.e.g.
Hello
我发现您遇到了三个问题:
nopic
变量的赋值存在语法错误。您需要为分配给它的值加上引号。.subX
元素内部没有任何文本。他们有html。因此,您应该使用.html()
方法而不是.text()
。.sub3
标记。 code> (我认为这是您用来测试的内容)。这是因为href
和class
属性颠倒了。这是工作的 JS,以及一个实例:
You've got three problems I can see:
nopic
variable. You need quotes around the value you're assigning to it..subX
elements don't have any text inside them. They have html. So you should be using the.html()
method rather than.text()
.<a>
tag in.sub3
(which I assume is what you're using to test). That's because thehref
andclass
attributes are reversed.Here's the working JS, as well as a live example:
我认为你可能会倒退。您想要做的是删除包含该链接标记的
div
。找到您要查找的a
并删除它的父级可能会更容易。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 thea
you're looking for and remove it's parent.