JQuery click handler for everything but a certain object

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

How do I check for a click on everything but a certain object?
I use (not), but I'm not sure how to call the class / id of the entire page.

$(".page").not("#divInfoBox").click(function (event) {  } 

What's the correct way in the HTML code to specify page, as I have something like:

<html> <body>     <div class="page">       </div> </body> </html> 

What I see is this: Clicking on the top half of the page works; but the bottom of the page, where there is nothing but background color, the click does not register because it is "off the page" -- how do I extend this so the click works everywhere?

Thanks, Michael

回答1:

You can add a click event to the "html" element. That way you get events even if you're "off" the page:

$("html").click( function() { ...  } ); 

Demo can be seen here: http://jsbin.com/eguti



回答2:

Try this:

$("body:not('#divInfoBox')").click(function (event) {  }); 

Read the doc page for the not selector:

http://docs.jquery.com/Selectors/not



回答3:

Your missing the ); at the end, should be like this:

$(".page").not("#divInfoBox").click(function (event) {     // do something }); 

As for your question, the 'page' div is not extending to the bottom of the page so you need to register the function to work on the body tag.

Also it looks like you are making a popup appear when certain things are clicked and disappear when clicked off to the side. I would recommend looking for a plugin that will handle this for you. I know there are several. Personally I like jqModal.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!