简体   繁体   English

触发父级div的点击事件

[英]Triggering click event of parent div


I have a issue where the .click from the div where my cancel button is nested in triggers when I click the button. 我有一个问题,当单击按钮时,来自div的.click来自我的“取消”按钮嵌套在触发器中的触发器。 This causes both the slideUp and slideDown to trigger at the same time, which results in the div staying visible. 这将导致slideUp和slideDown同时触发,从而使div保持可见。 I've tried adding a state to prevent the div from sliding down again, but this does not have the desired effect. 我尝试添加一个状态以防止div再次向下滑动,但这没有达到预期的效果。

$(add).click(function () {
 var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
 if(state == 0){
   $(inputDiv).slideDown(300);
  }
 state = 1;
});
 $(cancel).click(function () {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).slideUp(300);
  state = 0;
});

https://jsfiddle.net/kkdoneaj/2/ https://jsfiddle.net/kkdoneaj/2/

Does anyone know how to work around this issue? 有谁知道如何解决此问题?

Use Event.stopPropagation() to stop the click from bubbling from the #cancel button to the parent #add div: 使用Event.stopPropagation()可以阻止点击从#cancel按钮冒泡到父级#add div:

$("#cancel").click(function(e) {
    e.stopPropagation();
    ...
});

https://jsfiddle.net/kkdoneaj/3/ https://jsfiddle.net/kkdoneaj/3/

you should put stop 你应该stop

Stop the currently-running animation on the matched elements. 在匹配的元素上停止当前正在运行的动画。

$(add).click(function () {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).stop().slideDown(300);
});
$("#cancel").click(function (e) {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).stop().slideUp(300);
  e.stopPropagation();
});

Others have commented with stopPropagation() , but you can also not expand #inputDiv unless it's already visible, like so: 其他人使用stopPropagation()进行了注释,但是除非它已经可见,否则您也无法展开#inputDiv ,例如:

$(function(){
  $("#add").click(function () {
    if($("#inputDiv").css('display') == 'none'){
      $("#inputDiv").slideDown(1000);
    }
  });
  $("#cancel").click(function () {
    $("#inputDiv").slideUp(1000);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM