最新消息: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 - also trigger change on click away - Stack Overflow

matteradmin9PV0评论

I have a basic JQuery script that changes a few divs when you click - thus showing them - via toggle.

<script type="text/javascript">
     $('#content_display').click(function() {
         $(this).toggleClass('selected');
         $('#content_display_selector_container').toggle();
     });
</script>

However - to call the even you need to click only on the first main div with the ID of "content_display".

My question is this: how can I hide these changes using JQuery if the user also clicks on BODY - i.e. if you click away, the divs go back to their original hidden state?

Thanks for helping a JQuery clutz!

I have a basic JQuery script that changes a few divs when you click - thus showing them - via toggle.

<script type="text/javascript">
     $('#content_display').click(function() {
         $(this).toggleClass('selected');
         $('#content_display_selector_container').toggle();
     });
</script>

However - to call the even you need to click only on the first main div with the ID of "content_display".

My question is this: how can I hide these changes using JQuery if the user also clicks on BODY - i.e. if you click away, the divs go back to their original hidden state?

Thanks for helping a JQuery clutz!

Share Improve this question asked May 23, 2011 at 12:13 JamisonJamison 2,3484 gold badges27 silver badges31 bronze badges 1
  • Bind a click handler to the document or body element. – Felix Kling Commented May 23, 2011 at 12:16
Add a ment  | 

2 Answers 2

Reset to default 5

Something like this should work:

$('body').click(function(e) {
    if (!$(e.target).is('#content_display')) {
        $('#content_display').removeClass('selected');
        $('content_display_selector_container').hide();
    }
});

Hey - found a way to do this - does anyone think there's a better way?

Here's the result:

<script type="text/javascript">
   var mousetrap = false;
   $('body').click(function() {
       if (mousetrap == false) {
          $('#content_display').removeClass('selected');
          $('#content_display_selector_container').hide();
        }
   });
   $('#content_display').hover(function() {
         mousetrap = true;
   },function(){
         mousetrap = false;
});
     $('#content_display').click(function() {
           $(this).toggleClass('selected');
           $('#content_display_selector_container').toggle();
     });
</script>
Post a comment

comment list (0)

  1. No comments so far