【问题标题】:How do I hide my original show button and allow click anywhere to hide my content?如何隐藏我的原始显示按钮并允许单击任意位置来隐藏我的内容?
【发布时间】:2012-11-26 03:59:15
【问题描述】:

我想弄清楚如何隐藏我原来的显示按钮。然后做到这一点,当我点击页面上的任何地方时,它会隐藏显示按钮显示的原始内容。

<script>
    $(function(){

        $(".content").hide();
        $('.more-btn').click(function(){
           $(this).parent().parent().siblings('.content').show();
           $(this).hide();
        });

    });
</script>

这里是 HTML

<section>
    <article class="toggle-box" id="toggle1">
        <aside class="info-rollover">
            <h3>Locavores</h3>
            <button class="more-btn">Show More</button>
        </aside>
    </article>
    <aside class="content">
        <img src="images/loca/1.jpg"/>
        <img src="images/loca/2.jpg"/>
        <img src="images/loca/3.jpg"/>
        <img src="images/loca/4.jpg"/>
        <img src="images/loca/5.jpg"/>
        <img src="images/loca/6.jpg"/>
        <img src="images/loca/7.jpg"/>
        <img src="images/loca/8.jpg"/>
        <img src="images/loca/9.jpg"/>
    </aside>
</section>

新的点击代码隐藏显示

                   $(document).click(function(){
                    $('.content:visible').hide();
                    $('.content>img:hidden').show(),$('body').css('cursor', 'default');
                    });

            });

【问题讨论】:

    标签: jquery click show-hide


    【解决方案1】:

    隐藏按钮后,您可以绑定到正文单击。

    $( '.more-btn').click( function(){
      var button=$( this ).hide(),
      content= $('.content' ).show();
      $( 'body' ).bind( 'click.more', function( e ){
        $( this ).unbind( 'click.more' );
        button.show();
        content.hide();
      });
    });
    

    【讨论】:

    • 该按钮已从代码中删除。这就是我添加帖子的原因,谢谢!
    【解决方案2】:

    jsBin demo

    $(function(){
    
        $(".content").hide();
        $('.more-btn').click(function( e ){
           e.stopPropagation(); // to lower "DOM layers"
           $(this).closest('section').find('.content').show();
           $(this).hide();
        });
    
        $(document).click(function(){
          $('.content:visible').hide();
          $('.more-btn:hidden').show();
        });
    
    });
    

    【讨论】:

    • 这很好用,只是一个关于为什么需要 e 的快速问题?这会在不停止传播的情况下工作吗?
    • 很高兴成功了!谢谢。关于event:花点时间自己尝试一下:jsbin.com/ozejib/3/edit
    • 嘿@Roxon 你太棒了,谢谢你的帮助,我补充了这个。我实际上决定我不想要一个按钮来打开和关闭我的 div。我试图弄清楚这一点并被困在这里。因为如果我单击任何内容区域,它不会做任何事情,我必须单击外部。这使得不可能在移动端关闭。 $(document).click(function(){ $('.content:visible').hide(); $('.content>img:hidden').show(),$('body').css( '光标', '默认'); }); });
    • 请在您卡住的地方设置一个演示并描述您的问题。
    • @ZakFerris 我不能让你编辑我的帖子 Zak,去这里jsbin.com 并设置一个演示。
    猜你喜欢
    • 2017-08-28
    • 2021-06-14
    • 2016-05-21
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多