检测最深的列表链接点击

时间:2015-10-06 14:37:21

标签: javascript jquery html wordpress

我搜索jQuery代码,检测点击ul中最深的链接,其他链接可能不引用(event.preventDefault())。我在Wordpress函数中使用它:

wp_list_categories(
    array( 
        'title_li' => '',
        'depth' => 0,
        'child_of' => $this_cat->cat_ID
    )
);

列表代码:

<ul>
    <li class="cat-item cat-item-25"><a href="http://localhost/wordpress/category/abc">ABC Category</a>
    <ul class="children">
        <li class="cat-item cat-item-26"><a href="http://localhost/wordpress/category/abc/xyz">XYZ Category</a></li>
    </ul>
</li>

1 个答案:

答案 0 :(得分:3)

以下是一种方法,可以检查ul中有li个孩子的点击链接:

$('li > a').on('click', function(e) {
    e.preventDefault();
    if( !$(this).parent().children('ul').length ) {
        console.log( 'This is the deepest link' );
    }
});

$('li > a').on('click', function(e) {
    e.preventDefault();
    if( !$(this).parent().children('ul').length ) {
        console.log( 'This is the deepest link' );
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li class="cat-item cat-item-25"><a href="http://localhost/wordpress/category/abc">ABC Category</a>
    <ul class="children">
        <li class="cat-item cat-item-26"><a href="http://localhost/wordpress/category/abc/xyz">XYZ Category</a></li>
    </ul>
    </li>
</ul>