jQuery如果下一个隐藏元素宽度类没有孩子做某事

时间:2013-08-31 15:09:35

标签: jquery html

我有这个HTML结构:

<ul>

    <li class="cat-item">
        <a href="link.html">Link</a>
    </li>

    <li class="subcat-item" style="display: none">
        <ul class="subcat-list">
        </ul>
    </li>

    <li class="cat-item">
        <a href="link2.html">Link2</a>
    </li>

    <li class="subcat-item" style="display: none">
        <ul class="subcat-list">

            <li class="subcat-list-item"><a href="link3.html">Link 3</a></li>
            <li class="subcat-list-item"><a href="link4.html">Link 4</a></li>
            <li class="subcat-list-item"><a href="link5.html">Link 5</a></li>

        </ul>
    </li>



</ul>

现在我想做什么:

当你点击这个.cat-item时,

if(li.cat-item之后的下一个元素,类.subcat-item有子ul.subcat-list,这个ul.subcat-list有子节点),li.subcat- item slideToggles并返回false(页面未重新加载)

否则当你单击这个.cat-item时会出现函数:window.location = this.href;

如此简单:如果category有子类别slideToggle带有子类别链接的列表。 如果类别没有子类别,请转到类别链接。

对此有何帮助?

//编辑

感谢您的回答。还有一个我无法处理的步骤。这是HTML:

 <ul>

     <li class="cat-item">

         <a href="link.html">Link</a>

     </li> <!-- cat-item -->

     <li class="subcat-item" style="display: none;">

         <ul class="subcat-list">        

         </ul> <!-- subcat-list -->

     </li> <!-- subcat-item -->

     <li class="cat-item">

         <a href="link.html">Link</a>

     </li> <!-- cat-item -->    

     <li class="subcat-item" style="display: none;">

        <ul class="subcat-list">

            <li class="subcat-list-item">

                <a href="link.html">Link</a>

            </li> <!-- subcat-list-item -->

            <li class="sub-subcat-item" style="display: none;">

                <ul class="sub-subcat-list">

                </ul> <!-- sub-subcat-list -->

            </li> <!-- sub-subcat-item -->

            <li class="subcat-list-item">

                <a href="link.html">Link</a>

            </li> <!-- subcat-list-item -->

            <li class="sub-subcat-item" style="display: none;">

                <ul class="sub-subcat-list">

                    <li class="sub-subcat-list-item">

                        <a href="link.html">Link</a>

                    </li> <!-- sub-subcat-list-item -->

                    <li class="sub-subcat-list-item">

                        <a href="link.html">Link</a>

                    </li> <!-- sub-subcat-list-item -->

                </ul> <!-- sub-subcat-list -->

            </li> <!-- sub-subcat-item -->

        </ul> <!-- subcat-list -->

     </li> <!-- subcat-item -->   

 </ul>

现在:当用户点击.subcat-list-item和下一个带有类的子元素时,.sub-subcat-item没有子节点(ul不带li),他应该去.subcat-list-item url。否则.sub-subcat-item应该是slideToggle。

2 个答案:

答案 0 :(得分:0)

尝试

jQuery(function($){
    $('.cat-item').filter(function(){
        return $(this).next('.subcat-item').has('li').length > 0
    }).click(function(e){
        e.preventDefault();
        $(this).next('.subcat-item').toggle();
    })
})

演示:Fiddle

答案 1 :(得分:0)

$('.cat-item').click(function (e) {
    var x = $(this).next('.subcat-item').children('.subcat-list').children('.subcat-list-item');
    console.log(x.length);
    if (x.length > 1) {
        e.preventDefault();
        x.closest('.subcat-item').slideToggle();
    }
});

DEMO

在Op更新问题后更新

$('.subcat-list-item').click(function (e) {
    var x = $(this).next('.sub-subcat-item').children('ul').children('li');
    console.log(x.length);
    if (x.length != 0) {
        e.preventDefault();
        $(this).next('.sub-subcat-item').slideToggle();
    }
});

DEMO