sitemap
知识分享网:一个值得你收藏的网站!
当前位置:网站首页 > bootstrap > Bootstrap标签插件:mytab

Bootstrap标签插件:mytab

作者:xuhongji发布时间:2020-08-01分类:bootstrap浏览:697评论:0


导读:bootstrap的标签插件简化版,8-11增加无须使用js,只要在html里面添加data-toggle="tab"即可。JQuery代码:(function...

bootstrap的标签插件简化版,8-11增加无须使用js,只要在html里面添加data-toggle="tab"即可。

JQuery代码:

(function( $ ) { 
      $.fn.myTab = function() { 
            var $this = $(this),
            $ul = $this.closest('ul'),
            selector = $this.attr('data-target'),
            $parent = $this.parent('li'),
            $target;
            if (!selector) {
		        selector = $this.attr('href')
		        selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
		    }
		    if($this.parent('li').hasClass('active')) return;
		    $parent.addClass('active').siblings().removeClass('active');
		    $target = $(selector);
		    $target.show().siblings().hide();
       }; 
       $('[data-toggle="tab"]').click(function(e){
       	e.preventDefault();
       	$(this).myTab();
       });
})( jQuery );

HTML结构:

<div class="tab">
	<ul class="tab-menu" id="J-mytab">
		<li class="active"><a href="#tab1" data-toggle="tab">tab1</a></li>
		<li><a href="#tab2" data-toggle="tab">tab2</a></li>
	</ul>
	<div class="tab-con">
		<div class="tab-box active" id="tab1">con1</div>
		<div class="tab-box" id="tab2">con2</div>
	</div>
</div>


CSS代码参考:

.tab-menu li{display: inline-block;}
.tab-menu .active{color:red;}
.tab-con .tab-box{display: none;}
.tab-con .active{display: block;}

如果不在页面使用data-toggle="tab"的方法,则可以使用以下js:

$('#J-mytab a').click(function(event) {	event.preventDefault();	$(this).myTab();
});




欢迎 发表评论:

bootstrap排行
«    2023年2月    »
12345
6789101112
13141516171819
20212223242526
2728
标签列表
网站分类
文章归档
最近发表
推荐文章