jQuery - rule for “does not contain element”

前端 未结 2 647
余生分开走
余生分开走 2020-12-28 15:08

I want to create a rule to target the a element in the following:

相关标签:
2条回答
  • 2020-12-28 15:53

    If you're matching the a do this:

    var result  = $('#root > li:not(:has(ul)) > a');
    
    • http://api.jquery.com/not-selector/
    • http://api.jquery.com/has-selector/

    If you want to allow deeper nested <ul> elements, and just want to check to make sure only the direct children don't have a <ul>, you could do this:

    var result  = $('#root > li:not(:has(> ul)) > a');
    

    EDIT:

    To have more than one selector at the same time, separate them with a comma inside the quotes:

    var result  = $('#root > li:not(:has(> ul)) > a, #someOtherElement');
    
    0 讨论(0)
  • 2020-12-28 16:00
    $('#root a').click(function(e) {
        if ( $(this).parents('ul').length > 1 ) {
           e.preventDefault(); // cancel action for anchors inside of #root who have multiple parent list elements 
        }
    });
    

    Change logic per requirement.

    0 讨论(0)
提交回复
热议问题