JavaScript hasClass忽略元素的类?

时间:2012-12-05 01:19:06

标签: javascript jquery filter jquery-isotope slidedown

我有一个Isotope过滤器菜单,我正在使用哈希历史记录,因此如果用户返回页面,过滤器仍会设置。因为我们使用.slideDown来扩展子菜单项,所以当你回到页面时,子菜单项会被隐藏,即使它们中的某些过滤器被选中了。

我正在尝试使用:

if ($('#option1').hasClass('.selected')) {
    $('.level-two').slideDown('fast');
}

但是,由于“选择”类是由jQuery过滤器(Isotope)生成的,因此它被忽略。

小提琴:http://jsfiddle.net/RevConcept/swT84/

HTML:

<nav>           

            <div id="options" class="combo-filters">

                <div class="option-combo location">

                    <ul class="filter option-set group level-one" data-filter-group="location"> 

                        <li class="hidden"><a href="#filter-location-any" data-filter-value="" class="selected">any</a></li>

                        <li><a id="option1" href="#filter-location-exterior" data-filter-value=".exterior" class="trigger-two">exterior</a></li>

                        <li><a id="option2" href="#filter-location-interior" data-filter-value=".interior" class="trigger-two">interior</a></li>

                    </ul>

                </div>

                <div class="option-combo illumination">

                    <ul class="filter option-set group level-two" data-filter-group="illumination"> 

                        <li class="hidden"><a href="#filter-illumination-any" data-filter-value="" class="selected">any</a></li>

                        <li><a href="#filter-illumination-illuminated" data-filter-value=".illuminated" class="trigger-three">illuminated</a></li>

                        <li><a href="#filter-illumination-non-illuminated" data-filter-value=".non-illuminated" class="trigger-three">non-illuminated</a></li>

                    </ul>

                </div>

                <div class="option-combo mount">

                    <ul class="filter option-set group level-three" data-filter-group="mount"> 

                        <li class="hidden"><a href="#filter-mount-any" data-filter-value="" class="selected">any</a></li>

                        <li><a href="#filter-mount-wall" data-filter-value=".wall">wall</a></li>

                        <li><a href="#filter-mount-ground" data-filter-value=".ground">ground</a></li>

                    </ul>

                </div>


            </div><!--end options-->

        </nav>

CSS:

header nav ul.level-two, header nav ul.level-three {
display:none;
}

JavaScript的:

$(function(){
    $('.level-one').hide().fadeIn('fast');
});


$(".trigger-two").one('click', function(){
    $(".level-two").slideDown('fast'); 
});


$(".trigger-three").one('click', function(){
    $(".level-three").slideDown('fast'); 
});


if ($('#option1').is('selected')) {
    $('.level-two').slideDown('fast');
}

2 个答案:

答案 0 :(得分:2)

我猜你错过了'。'

if ($('#option1').is('.selected')) {
    $('.level-two').slideDown('fast');
}

答案 1 :(得分:1)

不是使用重复的功能复制每次点击的相同过程,而是始终以编程方式触发.selected锚点的点击。

$('.option-combo a.selected').trigger('click');

小提琴:http://jsfiddle.net/jgkwd/