简体   繁体   English

如何使div隐藏onclick并在再次单击时显示它?

[英]How do I make a div hide onclick and make it show when it's clicked again?

I have this code: 我有以下代码:

<script>
(function ($) {
    $(document).ready(function () {
        $("#thisclick, #thisclick2").click(function () {
            if ($('.example').is(":hidden")) {
                $(this).html($(this).html().replace(/Hide/, 'Show'));
            } else {
                $(this).html($(this).html().replace(/Show/, 'Hide'));
            }
            // Do it afterwards as the operation is async
            $(".example").hide();
        });
    });
})(jQuery);
</script>

The way the current code works is that if #thisclick is clicked it hides #example . 当前代码的工作方式是,如果单击#thisclick ,则会隐藏#example Which is what I require but when #thisclick is clicked again i want it to show #example . 这是我所需要的,但是当再次单击#thisclick时,我希望它显示#example Using the above code, it won't work. 使用上面的代码,它将无法正常工作。 What must I do to achieve this? 我必须怎么做才能做到这一点?

You should be able to change your code to as follows to get it to work: 您应该可以将代码更改为如下所示,以使其正常工作:

(function ($) {
    $(document).ready(function() {
    $("#thisclick").click(function () {
        $("#example").slideToggle("slow");
        });
    });
})(jQuery);

Here is a link to a quick sample: http://jsfiddle.net/andyjmeyers/szmXp/ 这里是一个快速示例的链接: http : //jsfiddle.net/andyjmeyers/szmXp/

Are you expecting like 你期望像

<button>This click</button>
<p style="display: none">Example</p>

<script>
$("button").click(function () {
$("p").toggle();
});
</script>

Please check it on http://jsfiddle.net/X5r8r/1107/ 请在http://jsfiddle.net/X5r8r/1107/上进行检查

<script>
    (function ($) {
          $(document).ready(function() {
          $("#thisclick").click(function () {
             if ($('#example').is(":hidden")) {
                 $(this).html($(this).html().replace(/Hide/, 'Show'));

             } else {
                 $(this).html($(this).html().replace(/Show/, 'Hide'));
             }
             // Do it afterwards as the operation is async
             $("#thisclick").slideToggle("slow");
             if($("#example").attr("isHidden") == "1")
                  $("#example").slideToggle("slow");
             $("#example").attr("isHidden","1");
          });
      });
        })(jQuery);
        </script>

You can do this: 你可以这样做:

$("#thisclick").click(function () {
   if ($('#example').hasClass("hidden"))
   {
      $('#example').removeClass("hidden");
      $('#example').show();
   }
   else
   {
      $('#example').addClass("hidden");
   }
}

or more easily: 或更容易:

$("#thisclick").click(function () {
   $('#example').toggleClass("hidden");
}

define style": 定义样式”:

.hidden
 { display:none;}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击不同的 div(我创建的菜单)时,如何显示和隐藏无序列表? - How do i make an unordered list show and hide when a different div (menu i created) is clicked? 单击div时如何隐藏div并显示另一个? - How do I hide a div when its clicked and show another? 单击时如何使按钮显示和隐藏文本以及为什么 getElement 不起作用? - How do I make a button show and hide text when clicked and why does getElement not work? 如何在单击按钮时使div的高度更大,然后再次单击时又变回其原始高度? - How can I make the height of a div bigger onclick of a button and then change back to it's original height when click again? 我该如何做 <div> 单击按钮时滑入视图? - How Do I make a <div> slide into view when a button is clicked? 显示div onclick并在其外部单击时隐藏div并关闭按钮 - show a div onclick and Hide div when clicked outside of it and close button 如何进行onclick切换以隐藏和显示视频播放器的UI? - How to make a toggle onclick to hide and show video player's UI? 如何隐藏和显示 <div> onclick,何时已有点击操作? - How to hide and show a <div> onclick when there's already an action for click? (JQUERY) 我想让一个不可见的 div 在点击时可见并在点击时再次隐藏它 - (JQUERY) I want to make an invisible div visible on click and hide it again onclick 单击时显示div,几秒钟后再次隐藏 - show div when clicked and hide again after a couple of seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM