简体   繁体   English

我认为stopPropagation()破坏了鼠标事件-如何解决?

[英]I think stopPropagation() is breaking my mouse events - how can I fix?

I am trying to detect clicks anywhere on the document only after my start session button has been clicked. 仅在单击开始会话按钮之后才尝试检测文档上任何位置的单击。 The start session button adds a .waiting class to the body . 开始会话按钮向body添加.waiting类。

I have used stopPropagation() , but I think this is breaking some mouseover/mouseout functionality that adds a hover state class to child elements. 我使用过stopPropagation() ,但是我认为这破坏了一些将鼠标悬停状态类添加到子元素的mouseover/mouseout功能。

The issue is that once the both the session button and document has been clicked, mouseover/mouseout functionality still happens despite removal of the .waiting class. 问题是,一旦单击了会话按钮和文档,即使删除了.waiting类, mouseover/mouseout功能仍然会发生。

What is the solution to fix my code please? 请问如何解决我的代码? Or, is stopPropagation() a bad idea for my use case given this article . 或者说,是stopPropagation()为给我的使用情况下,一个坏主意这篇文章

There is a snippet below - just click the button, hover over the squares and then click. 下面有一个代码段-只需单击按钮,将鼠标悬停在正方形上,然后单击即可。 The problem is the squares still have red outline on hover. 问题在于,悬停时正方形仍具有红色轮廓。

 $("#start-session").on("click", function(e) { $("body").addClass("waiting"); $("body.waiting").children().mouseover(function(e) { e.stopPropagation(); $(".el-hover").removeClass("el-hover"); $(e.target).addClass("el-hover"); }).mouseout(function(e) { $(e.target).removeClass("el-hover"); }); e.stopPropagation(); }) $(document).on("click", function(e) { if ($("body").hasClass("waiting")) { e.preventDefault() alert("Document was clicked"); $("body").removeClass("waiting") } }) 
 #container { width: 200px; background: stealblue; display: flex; flex-wrap: nowrap; justify-content: space-evenly; margin: 50px; } .zone { background: #eee; border: 1px solid #ccc; padding: 15px; } .el-hover { outline: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="start-session">Start session</button> <div id="container"> <div class="zone"></div> <div class="zone"></div> <div class="zone"></div> </div> 

Solution

To quote @CBroe from the comments: 引用@CBroe的评论:

$("body.waiting").children().mouseover(...) applies the handler to all elements found at the time - the handler does not magically get removed, once body loses that class again. $("body.waiting").children().mouseover(...)将处理程序应用于当时找到的所有元素-一旦正文再次丢失该类,该处理程序就不会神奇地被移除。 You're trying to work against that by stopping propagation, but I think you'd be better off applying the handler only if that class is currently present to begin with. 您正在尝试通过停止传播来解决该问题,但我认为,仅当当前存在该类开始时,才最好应用该处理程序。 Using event delegation you could achieve that kind of “dynamism” - but for that you would have to go one level up here, so that you can use body in the selector - something like $(document).on('mouseover', 'body.waiting *', function(...) {}) 使用事件委派可以实现这种“动态性”-但为此您必须在这里上$(document).on('mouseover', 'body.waiting *', function(...) {}) ,以便可以在选择器中使用$(document).on('mouseover', 'body.waiting *', function(...) {})类似于$(document).on('mouseover', 'body.waiting *', function(...) {})

Updated snippet 更新的代码段

 $("#start-session").on("click", function(e) { $("body").addClass("waiting"); $(document).on({ mouseenter: function() { $(this).css({ "outline": "1px solid red" }) }, mouseleave: function() { $(this).css({ "outline": "0" }) } }, 'body.waiting *'); e.stopPropagation(); }) $(document).on("click", function(e) { if ($("body").hasClass("waiting")) { e.preventDefault() alert("Document was clicked"); $("body").removeClass("waiting") } }) 
 #container { width: 200px; background: stealblue; display: flex; flex-wrap: nowrap; justify-content: space-evenly; margin: 50px; } .zone { background: #eee; border: 1px solid #ccc; padding: 15px; } .el-hover { outline: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="start-session">Start session</button> <div id="container"> <div class="zone"></div> <div class="zone"></div> <div class="zone"></div> </div> 

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

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