我正在尝试使我的 css 选项卡与移动设备兼容,尤其是 IOS。我还想在我的页面上使用 3 个不同的选项卡。我没有jquery知识,所以我试图找到一个例子。我发现的最好的例子是使用这种结构;
<ul id="tabs">
<li><a href="#" name="tab1">One</a></li>
<li><a href="#" name="tab2">Two</a></li>
<li><a href="#" name="tab3">Three</a></li>
<li><a href="#" name="tab4">Four</a></li>
</ul>
<div id="content">
<div id="tab1">...</div>
<div id="tab2">...</div>
<div id="tab3">...</div>
<div id="tab4">...</div>
</div>
它的jQuery是;
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$("#content div").hide(); // Initially hide all content
$("#tabs li:first").attr("id","current"); // Activate first tab
$("#content div:first").fadeIn(); // Show first tab content
$('#tabs a').click(function(e) {
e.preventDefault();
if ($(this).closest("li").attr("id") == "current"){ //detection for current tab
return
}
else{
$("#content div").hide(); //Hide all content
$("#tabs li").attr("id",""); //Reset id's
$(this).parent().attr("id","current"); // Activate this
$('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
}
});
});
</script>
有一个小问题。我不想将 id 用于标签。我用类更改了 id,然后修改了 CSS。我试图修改脚本,但它不起作用:(我的页面上有三个不同的选项卡组,这就是我将其更改为类的原因。我的新选项卡是;
<ul class="tabs">
<li><a href="#" name="tab1">One</a></li>
<li><a href="#" name="tab2">Two</a></li>
<li><a href="#" name="tab3">Three</a></li>
<li><a href="#" name="tab4">Four</a></li>
</ul>
<div class="tabs_content">
<div id="tab1">...</div>
<div id="tab2">...</div>
<div id="tab3">...</div>
<div id="tab4">...</div>
</div>
并尝试将 jQuery 更改为;
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$(".tabs_content div").hide(); // Initially hide all content
$(".tabs li:first").attr("id","current"); // Activate first tab
$(".tabs_content div:first").fadeIn(); // Show first tab content
$('.tabs a').click(function(e) {
e.preventDefault();
if ($(this).closest("li").attr("id") == "current"){ //detection for current tab
return
}
else{
$(".tabs_content div").hide(); //Hide all content
$(".tabs li").attr("id",""); //Reset id's
$(this).parent().attr("id","current"); // Activate this
$('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
}
});
});
</script>
你能帮我用jQuery吗?或者给我一个更好的例子来展示同一页面中的多个标签表?提前感谢
编辑:当我使用一个选项卡组时,现在随着更改,其他选项卡组的内容被隐藏:)
这是CSS文件;
.tabs{
overflow: hidden;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
}
.tabs li{
float: left;
margin: 0 .5em 0 0;
}
.tabs a{
position: relative;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
padding: .7em 3.5em;
float: left;
text-decoration: none;
color: #444;
text-shadow: 0 1px 0 rgba(255,255,255,.8);
border-radius: 5px 0 0 0;
box-shadow: 0 2px 2px rgba(0,0,0,.4);
}
.tabs a:hover,
.tabs a:hover::after,
.tabs a:focus,
.tabs a:focus::after{
background: #fff;
}
.tabs a:focus{
outline: 0;
}
.tabs a::after{
content:'';
position:absolute;
z-index: 1;
top: 0;
right: -.5em;
bottom: 0;
width: 1em;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
box-shadow: 2px 2px 2px rgba(0,0,0,.4);
transform: skew(10deg);
border-radius: 0 5px 0 0;
}
.tabs #current a,
.tabs #current a::after{
background: #fff;
z-index: 3;
}
.tabs_content
{
background: #fff;
padding: 2em;
height: 220px;
position: relative;
z-index: 2;
border-radius: 0 5px 5px 5px;
box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);
}
My new settings are;
Script is;
<script type="text/javascript" src="jquery-1.8.1.js"></script>
<script>
$(function(){
//INIT IS NOT NECESSARY IF YOU FOLLOW MY CODE
$("#tabs a").click(function(){
//ADD ACTIVE CLASS
$(this).parent().siblings().removeClass("current");
$(this).parent().addClass("current");
//SHOW CONTENT
$(".content-holder").removeClass("current");
var myIndex = $(this).parent().index(); // gets the index of the LI clicked
$(".content-holder").eq(myIndex).fadeIn();
});
});
</script>
HTML is;
<ul class="tabs">
<li class="tab current"><a href="#" name="tab1">One</a></li>
<li class="tab"><a href="#" name="tab2">Two</a></li>
<li class="tab"><a href="#" name="tab3">Three</a></li>
<li class="tab"><a href="#" name="tab4">Four</a></li>
</ul>
<div class="content">
<div class="content-holder current" id="tab1">content1</div>
<div class="content-holder" id="tab2">content2</div>
<div class="content-holder" id="tab3">content3</div>
<div class="content-holder" id="tab4">content4</div>
</div>
and my css is;
.tabs{
overflow: hidden;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
}
.tabs li{
float: left;
margin: 0 .5em 0 0;
}
.tabs a{
position: relative;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
padding: .7em 3.5em;
float: left;
text-decoration: none;
color: #444;
text-shadow: 0 1px 0 rgba(255,255,255,.8);
border-radius: 5px 0 0 0;
box-shadow: 0 2px 2px rgba(0,0,0,.4);
}
.tabs a:hover,
.tabs a:hover::after,
.tabs a:focus,
.tabs a:focus::after{
background: #fff;
}
.tabs a:focus{
outline: 0;
}
.tabs a::after{
content:'';
position:absolute;
z-index: 1;
top: 0;
right: -.5em;
bottom: 0;
width: 1em;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
box-shadow: 2px 2px 2px rgba(0,0,0,.4);
transform: skew(10deg);
border-radius: 0 5px 0 0;
}
.tabs #current a,
.tabs #current a::after{
background: #fff;
z-index: 3;
}
.content
{
background: #fff;
padding: 2em;
height: 220px;
position: relative;
z-index: 2;
border-radius: 0 5px 5px 5px;
box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);
}
.content-holder {
display:none;
}
.content-holder.current {
display:block;
}