Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 检查可用元素的if语句_Jquery - Fatal编程技术网

Jquery 检查可用元素的if语句

Jquery 检查可用元素的if语句,jquery,Jquery,我总是在这里找到答案,只是在阅读这些帖子,但现在是我的第一篇帖子的时候了,因为我找不到相关的问题: 这是我的目标代码: <li> <span class="panel"> <a href="…" class="image" rel="group"> <img title="…" src="…" alt="…" width="128" h

我总是在这里找到答案,只是在阅读这些帖子,但现在是我的第一篇帖子的时候了,因为我找不到相关的问题: 这是我的目标代码:

<li>
                <span class="panel">
                    <a href="…" class="image" rel="group">
                        <img title="…" src="…" alt="…" width="128" height="96" />
                    </a>
                    <span class="sizes">
                        <span class="button">
                            <a href="#" class="size1" rel="…" title="1">9x13</a>
                        </span>
                        <span class="button">
                            <a href="#" class="size2" rel="…" title="3">10x15</a>
                        </span>
                        <span class="button">
                            <a href="#" class="size3" rel="…" title="6">20x30</a>
                        </span>
                    </span>
                </span>
                <a href="…" class="image" rel="group">
                    <span class="added"></span>
                    <img title="…" src="…" alt="…" width="128" height="96" class="thumbnail" />
                </a>
</li>
当然我的if语句是废话。用我自己的话来说: 如果跨度(类别=面板)中有多个跨度(类别=移除),则 什么都不应该发生。 但是如果span(class=panel)中只剩下一个span(class=remove),那么 应该隐藏起来

我如何做到这一点?这里有一个在线演示:正如你所看到的,每当我点击图像下方的按钮,图像就会被标签标记。当没有选择按钮时,我想隐藏这个标签(class=added)

多谢各位! 来自德国的最美好祝愿

$("span.panel").find("span.remove").length
或者只是

$("span.panel span.remove").length

您可以将最后一位重写为以下内容:

$(".remove").live('click', function(){
            $(this).hide();
            $(this).prev().hide(); // hide checked span
             if ($(this).parent().parent().children().children().each().hasClass('checked'))
             { 
                $(this).parent().parent().parent().next().children(".added").hide();
             } 
        }
    );
$('.remove').live('click'), function() {
    if($(this).parents('.panel').find('.remove').length > 1)
        return;
    // there's only 1 - add the rest of the functionality here
}
虽然.parents()和.find()非常昂贵,但是如果没有很多这样的元素,开销可以忽略不计

此外,如果“删除”元素始终是同级元素,请将第二行的值更改为:

if($(this).siblings('.remove').length)

以上答案是正确的。但正如我所看到的,您并没有从DOM中删除这些“span.remove”,所以一旦添加它们,它将依赖于所有的情况。所以你也要检查他们的能见度

if($(this).parent().parent().children().find('.remove:visible').length == 0){
   $(this).parent().parent().parent().next().children(".added").hide();
}
这将是你的状况。当最后一个“.remove”被单击为隐藏时,这将隐藏该黄色条

正如你刚才问的,如何隐藏那个酒吧,这个就是这样。还有其他的事情你必须考虑,这可能是有缺陷的,比如“重新显示。如果再次选择的话,增加条形”,逻辑也似乎被打破了。
祝你好运。

毫无疑问,但是你是否可以分享一个关于jQuery选择器扩展性的资源?不确定你所说的扩展性是什么意思,但你可以始终使用jQuery文档页面作为参考:哇,你们救了我一天。两个主要的想法对我帮助很大:1。查找('.remove:visible')和2。长度==0这太简单了,但我完全不知道。非常感谢:-)很高兴知道这一点。但在StackOverflow,我们通过接受人们的回答来感谢他们。
if($(this).parent().parent().children().find('.remove:visible').length == 0){
   $(this).parent().parent().parent().next().children(".added").hide();
}