2

我有一组 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 菜鸟,所以我相信这会有点简单。

提前致谢!

4

5 回答 5

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

演示:小提琴

于 2013-03-12T03:11:13.820 回答
2

您可以通过绑定到documentclick事件并通过模拟单击活动选项卡来折叠活动选项卡来做到这一点。

$(document).click(function(e) {
    // make sure the click is outside the tabs
    if($(e.target).parents('#tabs').length == 0) {
        $('#tabs ul li.ui-state-active a').trigger('click');
    }
});

检查这个演示:http: //jsfiddle.net/SdfkD/

于 2013-03-12T03:09:33.450 回答
0

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

http://jqueryui.com/accordion/

于 2013-03-12T03:03:28.773 回答
0

它并不像你想象的那么简单。在 jQuery 中,您检测 DOM 的单个元素的点击,因此要确定是否有人在给定区域之外点击,您必须将点击事件绑定到整个主体,然后单独处理单独的可点击区域。

(将单击绑定到整个主体可能不是最好的主意,因此您可能需要重新设计选项卡的功能。当用户单击其外部的任何位置时,选项卡是否真的需要折叠?或者只是单击在同一标题上折叠选项卡工作?如果是这样,请使用@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( ...... ) ) {

  }
});
于 2013-03-12T03:38:11.460 回答
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>

于 2015-10-02T05:05:35.017 回答