如何在jquery中获取单击控件的ID?

时间:2013-02-19 05:55:14

标签: jquery

当我点击按钮显示/隐藏切换按钮时,我有一个切换div和一个按钮。

我使用此代码,当点击页面的任何位置时隐藏切换div。

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

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

我的问题是:当在切换按钮中单击控件时,此功能运行并隐藏切换div。

我想获得点击控件的ID。如果控制是切换div则不运行此功能。

2 个答案:

答案 0 :(得分: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");
         }
       });
     });

这肯定会对你有用。我希望你能提出的问题。

干杯。

答案 1 :(得分: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");
     }
  });
相关问题