最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jquery - Prevent click event from being triggered through an overlying layer - Stack Overflow

matteradmin8PV0评论

.PopBgd is 100% of the screens size with another div .PopUp contained within .PopBgd and appearing on top of it. Clicking on .PopBgd gives the desired effect of Hiding. However clicking anywhere in PopUp also runs the fadeOut part of the script below.

QUESTION How to prevent the fadeOut part of the script from triggering though overlying divs?

$('.BtnPop').click(function(e) {
    e.preventDefault();
    $($(this).data('popup')).fadeIn();
    $('.PopClose, .PopBgd').click(function() {
    $('.PopBgd').fadeOut();});
});

ANSWER

<button type="button" class="BtnPop" data-popup=".Pop1">CLICK</button>

<div class="Pop1 PopBgd">
  <div class="PopUp">
    <a class="PopClose">&#215;</a>
    <div>Content</div>
  </div>
</div>

$('.BtnPop').click(function(e) {
    e.preventDefault();
    $($(this).data('popup')).fadeIn();
});
$('.PopClose, .PopBgd').click(function() {
$('.PopBgd').fadeOut();});

$('.PopUp').click(function(e) {
    e.stopPropagation();
});

NEW QUESTION How to use StopPropogation when the target div's name is unknown? What I have tried above does not work.

I resolved my additional problem by simply adding a second class name that was static to the desired div to allow stopPropogation to work as normal.

.PopBgd is 100% of the screens size with another div .PopUp contained within .PopBgd and appearing on top of it. Clicking on .PopBgd gives the desired effect of Hiding. However clicking anywhere in PopUp also runs the fadeOut part of the script below.

QUESTION How to prevent the fadeOut part of the script from triggering though overlying divs?

$('.BtnPop').click(function(e) {
    e.preventDefault();
    $($(this).data('popup')).fadeIn();
    $('.PopClose, .PopBgd').click(function() {
    $('.PopBgd').fadeOut();});
});

ANSWER

<button type="button" class="BtnPop" data-popup=".Pop1">CLICK</button>

<div class="Pop1 PopBgd">
  <div class="PopUp">
    <a class="PopClose">&#215;</a>
    <div>Content</div>
  </div>
</div>

$('.BtnPop').click(function(e) {
    e.preventDefault();
    $($(this).data('popup')).fadeIn();
});
$('.PopClose, .PopBgd').click(function() {
$('.PopBgd').fadeOut();});

$('.PopUp').click(function(e) {
    e.stopPropagation();
});

NEW QUESTION How to use StopPropogation when the target div's name is unknown? What I have tried above does not work.

I resolved my additional problem by simply adding a second class name that was static to the desired div to allow stopPropogation to work as normal.

Share Improve this question edited Mar 4, 2013 at 15:53 DreamTeK asked Mar 4, 2013 at 14:15 DreamTeKDreamTeK 34.4k29 gold badges124 silver badges178 bronze badges 2
  • 1 e.StopPropagation() may help – bPratik Commented Mar 4, 2013 at 14:18
  • e.StopPropagation() does not work in my situation as I cannot declare the divs name as it is variable. What I tried above does not work for me. – DreamTeK Commented Mar 4, 2013 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 6
$('.Pop').click(function(e) {
    e.stopPropagation();
});

.stopPropagation() "Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event."

Post a comment

comment list (0)

  1. No comments so far