jQuery单击页面中除1 div之外的任何位置 [英] jQuery click anywhere in the page except on 1 div

查看:196
本文介绍了jQuery单击页面中除1 div之外的任何位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击网页上的任何位置(除了一个div(id = menu_content))时,如何触发某个函数?

How can I trigger a function when I click anywhere on my page except on one div (id=menu_content) ?

非常感谢

推荐答案

您可以在上应用 如果点击事件由id为的div生成,则取消 menu_content ,这会将事件绑定到单个元素并保存 menu_content c>

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

});

这篇关于jQuery单击页面中除1 div之外的任何位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆