Use JQuery to check a checkbox in a parent list-item?

后端 未结 4 914
刺人心
刺人心 2021-01-13 09:41

I\'m brand new to Javascript and JQuery, so I\'ve been reading up on it and am trying to check (and set inactive) a checkbox in a parent list-item when one of the children a

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-13 10:03

    the .parent() method returns only the immediate parent DOM element of your current selector. The .parents() method will search up the DOM tree to find any parent elements that match the selection. So $(".bar").parents(".foo") would select for all parent elements with Class "foo" of any elements with class "bar".

    First give your target parent element an identifable attribute, either a class or an id. If you were dealing with IDs, you wouldn't need parents(), so use a class.

    $(".child").click(function(){
      $(this).parents('.parentclass').toggle();
    });
    

提交回复
热议问题