单击div外部时折叠jquery选项卡

时间:2013-03-12 02:57:43

标签: jquery tabs

我有一组jQuery选项卡,我默认将它们折叠。我想要做的是在div外面点击时让它们崩溃。到目前为止,这是我的代码:

    <div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nunc tincidunt</a></li>
    <li><a href="#tabs-2">Proin dolor</a></li>
    <li><a href="#tabs-3">Aenean lacinia</a></li>
  </ul>
  <div id="tabs-1">
    <p><strong>Click this tab again to close the content pane.</strong></p>
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
  </div>
  <div id="tabs-2">
    <p><strong>Click this tab again to close the content pane.</strong></p>
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellenter code hereentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  </div>
  <div id="tabs-3">
    <p><strong>Click this tab again to close the content pane.</strong></p>
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
  </div>
</div>

这是我的jquery:

$(function() {
$( "#tabs" ).tabs({
        selected: -1,
        collapsible: true,
});
})

我是一个jQuery noob,所以我相信这会有点简单。

提前致谢!

5 个答案:

答案 0 :(得分:3)

试试这个

$(function() {
    $( "#tabs" ).tabs({
            selected: -1,
            collapsible: true,
    });
});

$('body').on('click', function(e){
    var target = $(e.target);
    if(!target.parents('#tabs').length){
        $( "#tabs" ).tabs('option', 'selected', -1);
    }
});

演示:Fiddle

答案 1 :(得分:2)

您可以通过绑定到document的{​​{1}}事件并通过模拟活动标签上的点击来折叠活动标签来执行此操作。

click

查看此演示:http://jsfiddle.net/SdfkD/

答案 2 :(得分:0)

试试这个jQuery UI而不是创建自定义的Accordian:

http://jqueryui.com/accordion/

答案 3 :(得分:0)

这并不像你想象的那么简单。在jQuery中,您可以检测DOM的各个元素的点击次数,因此要确定是否有人点击了某个区域的外部,您必须将click事件绑定到整个正文,然后处理单独分开可点击区域。

(将点击绑定到整个机身可能不是速度最好的想法,因此您可能需要重新设计选项卡的功能。当用户点击其外的任何位置时,标签是否真的需要折叠?或者只需单击相同的标题即可折叠标签工作?如果是这样,请使用@Dead Man建议使用jQuery手风琴。)假设您确实需要以您描述的方式实现标签,您可以尝试以下方法:

使用HTML的修改版本:

<div id="tabs" >
  <div class"expander">Click to expand Tab1</div>
  <div class="tab-text">Here is the text that appears when Tab1 is expanded</div>
  <div class"expander">Click to expand Tab2</div>
  <div class="tab-text">Here is the text that appears when the Tab2 is expanded</div>
</div>

你需要一些像这样的jQuery:

$( 'body' ).click( function( e ) {
  var $target = e.target;
  if( $target.is( '#tabs > .expander' ) ) {
    // We show a given tab when the user clicks on the heading to reveal it.
    $target.next().show();

    // You'll also need to decide what to do with the .expander itself.  
    // (You could change its text using .text( 'Click to hide' ) or you 
    // could just hide it until the user clicks outside all the tabs.

  } else if ( !( $target.is( '#tabs' ) || $target.is( '#tabs > div' ) ) ) {
    // If the user clicks outside of all the tabs, we hide them.
    $( '#tabs > .tab-text' ).hide();
  }

  // Add all other click event handlers for the page here.  
  if( $target.is( ...... ) ) {

  }
});

答案 4 :(得分:0)

您可以创建自己的自定义手风琴:

	$('.accordionOm').next().hide();
	$(".opened").next().show();
	$('.accordionOm').click(function()
	{
		if($(this).hasClass("opened") == true)
		{
			$(this).next().slideUp("slow");
			$(this).removeClass('opened');
		}
		else
		{
			$(".opened").next().slideUp("slow");
			$(".opened").removeClass("opened");
			$(this).addClass('opened');
			$(this).next().slideDown("slow");
		}
	});
.accordionOm { position:relative; padding:10px 0 10px 30px; margin:0; font:300 18px 'Oswald', Arial, Helvetica, sans-serif; cursor:pointer;}
.accordionOm:hover { color:#000;}
.accordionOm:before,.accordionOm:after { content:""; position:absolute; background:#333; display:inline-block;}
.accordionOm:before { width:20px; height:2px; left:0; top:22px;}
.accordionOm:after { width:2px; height:20px; left:9px; top:13px; transition:transform .5s; transform:rotate(0);}
.accordionOm.opened:after { transform:rotate(90deg);}
.accordionOm + div { border-left:4px solid #999; padding:0 15px; margin-left:8px; font:13px 'Open Sans', Arial, Helvetica, sans-serif; color:#666;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4 class="accordionOm opened">Accordian heading</h4>
<div>
	<p>Lorem ipsum dolor sit amet, adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus.</p>
</div>

<h4 class="accordionOm">Accordian heading</h4>
<div>
	<p>Lorem ipsum dolor sit amet, adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus.</p>
</div>

<h4 class="accordionOm">Accordian heading</h4>
<div>
	<p>Lorem ipsum dolor sit amet, adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus.</p>
</div>

<h4 class="accordionOm">Accordian heading</h4>
<div>
	<p>Lorem ipsum dolor sit amet, adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus.</p>
</div>