如何打开/关闭 $(document).click() 事件或其他解决方案

发布于 2024-12-18 16:26:02 字数 200 浏览 1 评论 0原文

我有一个 DIV 标签,单击该标签时我想启动一个进程,然后单击文档上的任意位置我想结束该进程。到目前为止,我已经尝试过:

$(document).click(end_process);
$('div_tag_id').click(start_process);

这不好,因为单击启动进程也会立即结束进程。有什么想法吗?

I have a DIV tag which when clicked I want to start a process, after which clicking anywhere on the document I want to end the process. So far I have tried this:

$(document).click(end_process);
$('div_tag_id').click(start_process);

This is no good as clicking to start the process also ends the process immediately afterwards. Any ideas?

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

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

发布评论

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

评论(5

情泪▽动烟 2024-12-25 16:26:02

我相信你想要这样的东西:

$('div_tag_id').click(function(e) {
   e.stopPropagation();
   start_process(e);
});

请参阅:

http://api.jquery.com/event.stopPropagation/

I believe you want something like this:

$('div_tag_id').click(function(e) {
   e.stopPropagation();
   start_process(e);
});

See:

http://api.jquery.com/event.stopPropagation/

习惯成性 2024-12-25 16:26:02

在处理程序中使用 stopPropagation

$('div_tag_id').click(function(e) {
    e.stopPropagation();
    start_process();
});

Use stopPropagation in your handler:

$('div_tag_id').click(function(e) {
    e.stopPropagation();
    start_process();
});
猥︴琐丶欲为 2024-12-25 16:26:02

当然,您只是想阻止事件在 start_process 处理程序中冒泡:

start_process(e) {
  e.stopPropagation();
}

编辑:看起来每个人都在同一时间说了同样的事情。祝你好运!

Sure, you just want to stop the event from bubbling up in your start_process handler:

start_process(e) {
  e.stopPropagation();
}

Edit: Looks like everyone said about the same thing at the same time. Good luck!

筑梦 2024-12-25 16:26:02

您可以在 start_process 函数中调用 .stopPropagation() 来阻止事件冒泡到 document

function start_process(event) {
    event.stopPropagation();
    //..your code here
}

这是 的文档>.stopPropagation(): http://api.jquery.com/event.stoppropagation/

You can call .stopPropagation() in the start_process function to stop the event from bubbling up to the document:

function start_process(event) {
    event.stopPropagation();
    //..your code here
}

Here is the documentation for .stopPropagation(): http://api.jquery.com/event.stoppropagation/

你对谁都笑 2024-12-25 16:26:02

使用 stopPropagation 阻止点击传播到父级。

$('div_tag_id').click(function(e){
    start_process();
    e.stopPropagation();
});

或者,只需将 stopPropagation 添加到 start_process 的主体中。

Use stopPropagation to stop the click from propagating to the parent.

$('div_tag_id').click(function(e){
    start_process();
    e.stopPropagation();
});

alternatively, just add stopPropagation into the body of start_process.

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