Better way to “find parent” and if statements

后端 未结 2 1368
太阳男子
太阳男子 2021-01-19 04:43

I can\'t figure out why this isn\'t working:

$(document).ready(function() {
    if ($(\'.checkarea.unchecked\').length) {
       $(this).parent().parent().pa         


        
相关标签:
2条回答
  • 2021-01-19 05:12

    wow, you can use closest or parents() method.

    closest( selector )Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

    parents( [selector] )Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.

    0 讨论(0)
  • 2021-01-19 05:36

    I'd suggest:

    $('.toggle-open-area').click(function() {
        $(this).toggleClass('unchecked checked');
        $(this).closest(appropriateSelector).toggleClass('open closed');
    });
    

    Incidentally, I can't provide an actual selector in place of appropriateSelector because your posted image1 doesn't (seem to) include that particular element.

    References:

    • closest().
    • toggleClass().

    1. You realise you could include the actual mark-up in the question? It's much more convenient that way, and far easier to work with (create demos from, correct typos, retain usefulness in the event the external site falls over, dies or reorganises its content...).

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