javascript - JavaScript 或 jQuery 可以检测事件传播何时完成吗?

标签 javascript jquery events propagation

JavaScript 或 jQuery 中是否有任何方法可以检测事件何时完成在 DOM 上的传播?我有一个 jQuery 单击处理程序,它在单击时附加(带有目标“htm​​l”),并且添加它的单击在它冒泡到目标后就会触发它。我想延迟处理程序附件,直到传播完成。我听说 event.stopPropagation() 是有风险的事情,所以我不想使用它。

这是我的代码。 #title-editable 是一个 anchor 标记。 vclick 是 jQuery Mobile 定义的事件。我只需单击 #title-editable 即可获得两个控制台日志。

  $("#title-editable").on("vclick", function(event){
    event.preventDefault();
    console.log("First handler fired.");
    /* Would like to detect completion of event propagation here 
      and only execute the next statement after that. */
    $("html").one("vclick",function(){
      console.log("Second handler fired.");
    });
  });

编辑:我想我有一个解决方法。我使用 .on 代替内部事件的 .one,这样事件在第一次触发时就不会分离。我检查点击事件的目标,仅执行内部事件的更改,如果目标不是 #title-editable,则分离内部事件。

  $("#title-editable").on("vclick", function(event){
    event.preventDefault();
    console.log("First handler fired.");
    $("html").on("vclick",function(event2){
      console.log("Second handler fired.");
      if(!$(event2.target).closest("#title-editable").length){
        console.log("Execute changes here.");
        $("html").off("vclick");
      }
    });

最佳答案

根据您最后的评论,最简单的逻辑是这样的:

var changed=false;
$('#title-editable').on('vclick',function(event){
    event.preventDefault();
    console.log("First handler fired.");
    changed=true;
});
$(document).on("vclick",function(){
    if(changed){
        console.log("Second handler fired.");
        //here you can revert your actions and then the next line
        changed=false;
    }
});

更新:

第二种理论:

您说过,祖先在单击#title-editable时会获得一个类,因此您可以尝试以下代码:

$(document).on("vclick",function(event){
    if($('#title-editable').parent().hasClass('givenClass')){ //assuming the class is called "givenClass" and the ancestor is the parent of "#title-editable" (you need to sort that out)
        console.log("Second handler fired.");
        //here you can revert your actions and then the next line
    }
    else{
        event.preventDefault();
        console.log("First handler fired.");
    }
});

第三种理论:

$(document).on("vclick",function(event){
    if(!$('#title-editable').is(':visible')){
        console.log("Second handler fired.");
        //here you can revert your actions and then the next line
    }
    else{
        event.preventDefault();
        console.log("First handler fired.");
    }
});

第四种理论:

$(document).on("vclick",function(event){
    if(!$('#title-editable').is(event.target) || !$('#title-editable').has(event.target)){
        console.log("Second handler fired.");
        //here you can revert your actions and then the next line
    }
    else{
        event.preventDefault();
        console.log("First handler fired.");
    }
});

关于javascript - JavaScript 或 jQuery 可以检测事件传播何时完成吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962562/

相关文章:

javascript - JSON Restful 服务安全

javascript - Angular $state.go() 不断重定向到 IE 中的索引

java - 回合制游戏设计 : Event-Driven vs. 游戏循环

javascript - iScroll 4 - 取消 PreventDefault()

javascript - stringByEvaluatingJavaScriptFromString 不会触发 JavaScript

javascript - 如何使用 javascript 将值 bootstrap 下拉列表设置为隐藏值 asp.net

javascript - 隐藏数据表导出功能和分页

javascript - 在数字计数器上添加文本

jquery - 如何使用 jquery 在单击时更改表格行的文本

c++ - 子类化 QPixmap