Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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/else语句_Jquery_Loops_If Statement_Each - Fatal编程技术网

Jquery 每个if/else语句

Jquery 每个if/else语句,jquery,loops,if-statement,each,Jquery,Loops,If Statement,Each,我有一个jQuery语句,我想通过类“test\u section\u holder”遍历所有div,并将if/else语句应用于最近的“sub\u ttl”div 这是我的jQuery——我将把.each放在哪里 $(function() { // This will fire when document is ready: $('.sub_ttl').click(function () { // This will fire each time the win

我有一个jQuery语句,我想通过类“test\u section\u holder”遍历所有div,并将if/else语句应用于最近的“sub\u ttl”div

这是我的jQuery——我将把.each放在哪里

$(function() {
    // This will fire when document is ready:
   $('.sub_ttl').click(function ()  {
        // This will fire each time the window is resized:
        if($(window).width() >= 980) {
            // if larger or equal
            if( $('#test').hasClass('desktop_refine') && $(".test_section_holder").is(":visible"))
                {
                    $(".test_section_holder").closest(".test_section").find(".sub_ttl").toggleClass("mobileopen").removeClass("on");
                }
            } else {
                // if smaller
                $('#test').addClass('mobile_refine').removeClass('desktop_refine');
        }
    }).resize();
});
这是我的HTML

<div id="test">
    <div class="test_section">
        <span class="sub_ttl">Sub title 1</span>
        <div class="test_section_holder">
            Section 1
        </div>
    </div>
    <div class="test_section">
        <span class="sub_ttl">Sub title 2</span>
        <div class="test_section_holder">
            Section 2
        </div>
    </div>
</div>

副标题1
第一节
副标题2
第二节
和CSS

<style>
#test {height:auto; width:100%; overflow:hidden; font-family:arial; text-indent:15px;}
#test .test_section {height:auto; width:100%; overflow:hidden;}
#test .test_section .sub_ttl {height:auto; width:100%; background:#000000; color:#ffffff; display:block; padding:15px 0;}
#test .test_section .test_section_holder {display:none; margin-bottom:20px; margin-top:10px;}
#test .test_section .sub_ttl.on {background:yellow; color:#000000;}

#test .test_section .sub_ttl.mobileopen {background:red;}
#test .test_section .sub_ttl.mobileclosed {background:green;}
#test .test_section .sub_ttl.desktopopen {background:blue;}
#test .test_section .sub_ttl.desktopclosed {background:pink;}

@media only screen and (min-width:980px){
#test {width:300px;}    
#test .test_section .test_section_holder {display:block;}
}
</style>

#测试{高度:自动;宽度:100%;溢出:隐藏;字体系列:arial;文本缩进:15px;}
#test.test_节{高度:自动;宽度:100%;溢出:隐藏;}
#test.test_section.sub_ttl{高度:自动;宽度:100%;背景:000000;颜色:ffffff;显示:块;填充:15px 0;}
#test.test_section.test_section_holder{显示:无;页边距底部:20px;页边距顶部:10px;}
#test.test_section.sub_ttl.on{背景:黄色;颜色:000000;}
#test.test_section.sub_ttl.mobileopen{背景:红色;}
#test.test_section.sub_ttl.mobileclosed{背景:绿色;}
#test.test_section.sub_ttl.desktopopen{背景:蓝色;}
#test.test_section.sub_ttl.desktopclosed{background:pink;}
@仅介质屏幕和(最小宽度:980px){
#测试{宽度:300px;}
#test.test_section.test_section_holder{display:block;}
}

有什么想法吗?

我想这就是你想要的:

if ($('#test').hasClass('desktop_refine') {
    $(".test_section_holder").is(":visible").each(function () {
        $(this).closest(".test_section").find(".sub_ttl")
          .toggleClass("mobileopen") 
          .removeClass("on");
    });
} else {
    // if smaller
    $('#test').addClass('mobile_refine').removeClass('desktop_refine');
}

有什么问题?发生了什么与您希望发生什么?我想根据if/else语句的条件向sub_ttl div添加一个类-if需要根据相关的test_节分别应用于每个sub_ttl div,所以您想用循环替换click方法,或者在click内循环?在click内循环。。。如果可能,有限可能。您似乎对jQuery有很好的理解。什么阻止了您?
.is()
返回
true
false
,而不是集合。使用
.filter()
而不是
.is()
,或者将选择器更改为
。测试部分\u保持架:可见