2

I have a toggle div and a button, when i clicking button show/hide toggle button.

I use this code for, when clicking anywhere of the page hide the toggle div.

  $(document).click(function () {
             var $el = $(".Search");

             // toggle div
             if ($el.is(":visible")) {
                 // fade out
                 $(".Search").toggle("slow");
             }
         });

My problem is: When clicking on control in toggle button , this function run and hide toggle div.

I want to get id of a clicked control. if control is toggle div don't run this function.

4

2 回答 2

0

你这里少了点什么。您应该使用以下代码:

$(document).ready(function () {
   $("button[name='give the name of button on which you want to toggle']").click(function(){
     var $el = $(".Search");

         // toggle div
         if ($el.is(":visible")) {
             // fade out
             $(".Search").toggle("slow");
         }
       });
     });

这肯定会为你工作。我希望是你所要求的。

干杯。

于 2013-02-19T06:00:23.890 回答
0

看到你必须.stopPropagation()

 $('togglebtn').click(function(e){ //<----put your btn's class or id
   e.stopPropagation(); // <---------this will stop the event to bubble to parent
   // your stuff to hide and show.
 });

 $(document).click(function () {
     var $el = $(".Search");
     if ($el.is(":visible")) {
       $(".Search").toggle("slow");
     }
  });
于 2013-02-19T06:01:33.913 回答