简体   繁体   English

停止将鼠标单击事件传播到父 div

[英]Stop propagation of mouse click event to parent div

I have a HTML structure like:我有一个 HTML 结构,如:

<div class="container">
    <div id="bg">   
        <img src="#"/>
        <div id="screen">
         <img src="#" id="draggable"/>
        </div>
    </div>   

</div>

The "bg" div is above the "screen" div (used z-index property to do so), i want to drag the image in screen div when i click on the "bg" div. “bg”div 位于“屏幕”div 上方(使用 z-index 属性这样做),当我单击“bg”div 时,我想在屏幕 div 中拖动图像。

I am able to do so, but the issue is i am getting the error: Uncaught RangeError: Maximum call stack size exceeded .我能够这样做,但问题是我收到错误: Uncaught RangeError: Maximum call stack size exceeded

I believe its due to the click event from "bg" i pass to "screen" using trigger.我相信这是由于我使用触发器传递给“屏幕”的“bg”的点击事件。 How can i stop the event i passed to "screen" from firing again on "bg".如何阻止我传递给“屏幕”的事件在“bg”上再次触发。

To make it clear, here is the JSFiddle of my code: Click Here为了清楚起见,这里是我的代码的 JSFiddle:点击这里

I believe you already solved your problem as your jsFiddle is working now, so I am gonna explain for others, so we can mark this question as solved.我相信你已经解决了你的问题,因为你的 jsFiddle 现在正在工作,所以我会为其他人解释,所以我们可以把这个问题标记为已解决。
Just move your div#screen out of div#bg to prevent the recursion of event bubbling.只需将div#screen移出div#bg即可防止事件冒泡的递归。

NOTE: To prevent confusion you should not update the contents of your link after you publish it.注意:为防止混淆,您不应在发布链接后更新其内容。

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

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