单击实时选择器外部

发布于 2024-08-28 05:15:42 字数 204 浏览 5 评论 0原文

如果用户单击页面上该元素之外的任何位置,我想使用 live 方法隐藏该元素。它与 clickoutside 插件 的功能完全相同,但使用的是 ajax 加载的元素。关于如何做到这一点有什么想法吗?

I want to use the live method to hide an element if the user clicks anywhere on the page outside of that element. It is exactly like what clickoutside plugin does, but with ajax loaded elements. Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多彩岁月 2024-09-04 05:15:42

像这样的东西

$("yourelementselector").live("click", function(){
    // your code
    return false; // prevents bubbling of event
});

$("body").click(function(){
   var yourElement = $("yourelementselector");
   if (yourElement.is(:visible))
   {
       yourElement.hide();
   }
});

Something like

$("yourelementselector").live("click", function(){
    // your code
    return false; // prevents bubbling of event
});

$("body").click(function(){
   var yourElement = $("yourelementselector");
   if (yourElement.is(:visible))
   {
       yourElement.hide();
   }
});
吐个泡泡 2024-09-04 05:15:42

您可以执行以下操作:

$("#myElement").live('click', function(){
   return false;
});
$("body").live('click', function(){
   $("#myElement").hide();
});

工作原理:如果您单击该元素,则单击事件不会冒泡,从而导致单击 元素。如果您在元素外部单击,它会冒泡,最终到达隐藏您的元素的

You can do this:

$("#myElement").live('click', function(){
   return false;
});
$("body").live('click', function(){
   $("#myElement").hide();
});

How it works: If you click on the element, the click event doesn't bubble up, causing a click on the <body> element. If you click outside the element though, it bubbles up, eventually getting to <body> which hides your element.

吃兔兔 2024-09-04 05:15:42

有一个适用于实时的 clickoutside 事件的修改版本的示例: http://benalman.com/news/2010/03/jquery-special-events/#highlighter_680190

There is an example a modified version of the clickoutside event that works with live: http://benalman.com/news/2010/03/jquery-special-events/#highlighter_680190

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文