单击内部div时,防止单击处理程序触发

时间:2014-04-25 18:17:57

标签: javascript jquery css user-interface google-apps-script

我相信我的问题与此问题相反:Prevent execution of parent event handler

我正在使用Google Apps脚本构建UI。我有一个选择框,在一个更大的容器div里面。我希望能够在单击父div时使用jQuery运行动画,但我不希望在单击选择框时运行动画。我尝试使用stopPropagation,但这没有帮助。目前,动画在div本身或单击选择下拉列表时运行:

  var ISDOWN = true;  //global to track whether div is down or up

  $("#outer-container").click(
  function(e) {
    e.stopPropagation();
    var source = $(this).attr('id');
    if (source === "outer-container"){
      if (ISDOWN === true){
        $("#outer-container").animate({top: "8px"});
          ISDOWN = false;
      }
      else {
        $("#outer-container").animate({top: "45px"});
          ISDOWN = true;
      }
    }
  });

1 个答案:

答案 0 :(得分:0)

您可以检查从事件对象

上的目标属性中单击的元素
if ($(e.target).attr('id') === 'outer-container') {
  if (ISDOWN === true){
    $("#outer-container").animate({top: "8px"});
      ISDOWN = false;
  }
  else {
    $("#outer-container").animate({top: "45px"});
      ISDOWN = true;
  }
}