Here is an example of how you can add and remove a class in jQuery based on click events:

$(document).on('click', function(e) {
  if (!$(e.target).closest('.addclass').length) {
    $('.addclass').removeClass('your-class');
  }
});

$('.addclass').on('click', function() {
  $(this).addClass('your-class');
});

In this example, we use the $(document).on('click') event to listen for any click events that occur on the document. We then check if the clicked element or any of its ancestors have the class addclass using $(e.target).closest('.addclass').length. If not, we remove the class your-class from all elements with the class addclass.

Additionally, we use the $('.addclass').on('click') event to listen for click events specifically on elements with the class addclass. When clicked, we add the class your-class to the clicked element.

Note: Make sure to replace 'your-class' with the actual class name you want to add or remove

click on class addclass and remove if click was not closest in jqueryif click outside of class remove class also please write by on click

原文地址: https://www.cveoy.top/t/topic/iHZM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录