且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery 单击页面中除 1 div 之外的任何位置

更新时间:2022-05-05 22:46:47

您可以在文档的body上应用click并取消click处理如果 click 事件是由 id menu_content 的 div 生成的,这会将事件绑定到单个元素并保存 click 与除 menu_content

You can apply click on body of document and cancel click processing if the click event is generated by div with id menu_content, This will bind event to single element and saving binding of click with every element except menu_content

$('body').click(function(evt){    
       if(evt.target.id == "menu_content")
          return;
       //For descendants of menu_content being clicked, remove this check if you do not want to put constraint on descendants.
       if($(evt.target).closest('#menu_content').length)
          return;             

      //Do processing of click event here for every element except with id menu_content

});