Better way to “find parent” and if statements

后端 未结 2 1369
太阳男子
太阳男子 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: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...).

提交回复
热议问题