gpt4 book ai didi

jquery - 带有 jquery 的 CSS 选项卡(兼容 ios)

转载 作者:行者123 更新时间:2023-11-28 13:41:58 26 4
gpt4 key购买 nike

我正在尝试使我的 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;
}

最佳答案

我设置了一个可以处理多个标签集的插件。

(function ($) {
$.fn.tabset = function () {
var $tabsets = $(this);
$tabsets.each(function (i) {
var $tabs = $('li.tab a', this);
$tabs.click(function (e) {
var $this = $(this);
panels = $.map($tabs, function (val, i) {
return $(val).attr('href');
});
$(panels.join(',')).hide();
$tabs.removeClass('selected');
$this.addClass('selected').blur();
$($this.attr('href')).show();
e.preventDefault();
return false;
}).first().triggerHandler('click');
});
};
})(jQuery);

您必须将 html 包装在包含选项卡和内容的 div 中。您还需要更新 anchor 以使用标准哈希标签,请参见下文:

<div class="tabset">
<ul class="tabs">
<li class="tab"><a href="#tab1">One</a></li>
<li class="tab"><a href="#tab2">Two</a></li>
<li class="tab"><a href="#tab3">Three</a></li>
<li class="tab"><a href="#tab4">Four</a></li>
</ul>

<div class="panel" id="tab1">...</div>
<div class="panel" id="tab2">...</div>
<div class="panel" id="tab3">...</div>
<div class="panel" id="tab4">...</div>
</div>

你可以这样调用它:

$('div.tabset').tabset();

您可以在以下位置查看包含三个选项卡集的工作示例:http://jsfiddle.net/rustyjeans/XVp5X/

关于jquery - 带有 jquery 的 CSS 选项卡(兼容 ios),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com