2012-11-26 24 views
2

我想弄清楚如何隐藏我原来的显示按钮。然后,当我点击页面上的任意位置时隐藏原来由show按钮显示的原创内容。如何隐藏我的原始演出按钮并允许点击任何位置以隐藏我的内容?

<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'); 
        }); 

      }); 

回答

0

一旦你隐藏的按钮,你可以绑定到身体的点击。

$('.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(); 
    }); 
}); 
+0

按钮已经从代码去除。那为什么我加到帖子里谢谢你! –

0

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(); 
    }); 

}); 
+0

这很好,只是一个关于为什么e是必要的简单问题?这可以在不停止传播的情况下工作? –

+0

很高兴工作!谢谢。关于'e'vent:花时间去尝试一下自己:http://jsbin.com/ozejib/3/edit –

+0

嘿@Roxon你真棒,感谢您的帮助,林添加到这一点。我实际上决定我不想要一个按钮来打开和关闭我的div。我试图解决这个问题,并卡在这里。因为如果我点击任何内容区域,它不会做任何事情,我必须点击外部。这使得无法关闭事物的移动端。 $(文件)。点击(函数(){ \t \t \t \t $( '内容:可见。 ')隐藏(); \t \t \t \t $(' 内容> IMG:隐藏'。)秀(),$( '主体')的CSS( '光标', '默认');。 \t \t \t \t}); \t \t \t \t}); –

相关问题