最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery disable click event for div - Stack Overflow

programmeradmin3浏览0评论

Let's say I got a simple click event for a HTML div. If I click on the div it should trigger a fadeToggle.

In that div I got another div with another trigger. On that click event it should do something but instead of doing the event it triggers the first event.

How do I prevent event 1 from triggering if I want event2 to trigger?

Event1 should trigger only if I don't press the clickevent on the second event everything else should trigger as usual.

Thanks for your time

HTML

<div id="1">
     Event 1
     <div id="2">
         <div id="3">
            but here is something as well event2
          </div>
     </div>
</div>

JavaScript

$("#1").click(function() {
   $(this).slideToggle();
});


$("#3").click(function() {
   $(this).css("background-color", "blue");
});

Fiddle: /

Let's say I got a simple click event for a HTML div. If I click on the div it should trigger a fadeToggle.

In that div I got another div with another trigger. On that click event it should do something but instead of doing the event it triggers the first event.

How do I prevent event 1 from triggering if I want event2 to trigger?

Event1 should trigger only if I don't press the clickevent on the second event everything else should trigger as usual.

Thanks for your time

HTML

<div id="1">
     Event 1
     <div id="2">
         <div id="3">
            but here is something as well event2
          </div>
     </div>
</div>

JavaScript

$("#1").click(function() {
   $(this).slideToggle();
});


$("#3").click(function() {
   $(this).css("background-color", "blue");
});

Fiddle: http://jsfiddle/VLcxJ/

Share Improve this question edited Feb 25, 2014 at 9:18 V31 7,6663 gold badges28 silver badges44 bronze badges asked Feb 25, 2014 at 9:12 AMEAME 2,3026 gold badges22 silver badges39 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2
$("#3").click(function(e) {
    e.stopPropagation();
    $(this).css("background-color", "blue");
});

After that, clicks on #3 won't reach #2 or #1

Use event.stopPropagation(); :

$("#e3").click(function(event) {
   event.stopPropagation();
   $(this).css("background-color", "blue");
});

See it working here : http://jsfiddle/bYn22/

Note from jQuery API (I couldn't explain it better) : event.stopPropagation() > Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

And note that your IDs can't begin with a number :)

$("#3").click(function(event) {
   event.stopPropagation();
   $(this).css("background-color", "blue");
});

Added Fiddle: http://jsfiddle/VLcxJ/1/

Here is the solution

        $("#1").click(function() {
            $(this).slideToggle();
        });


        $("#3").click(function(event) {
           event.stopPropagation();
            $(this).css("background-color", "blue");
        });

http://jsfiddle/tR7h9/3/

发布评论

评论列表(0)

  1. 暂无评论