gpt4 book ai didi

javascript - 在选项卡上使用 Javascript 的事件状态

转载 作者:行者123 更新时间:2023-11-28 06:43:57 25 4
gpt4 key购买 nike

有没有一种方法可以使用 Javascript,以便在默认情况下加载页面时,它的第一个选项卡颜色为深绿色。此外,一旦您单击选项卡,事件状态就会变为深绿色。我在 CSS 中设置了它,以便悬停状态变为深绿色,但我需要使用 JavaScript 的事件状态。

非常感谢您的帮助!我从中删除了很多代码。

.tabs_accordion {
display: block;
margin: 0 auto;
max-width: 1200px;
}


.tabs_accordion > input {
/*position: relative;
left: -50000px;
height: 0px;
line-height: 0;*/
display: none;
}

.tabs_accordion > input:nth-child(1):checked ~ ul.tabs > li:nth-of-type(1) {
font-weight: bold;
}
.tabs_accordion > input:nth-child(1):checked ~ div.content > label:nth-of-type(1) {
font-weight: bold;
margin-bottom: 0;
}
.tabs_accordion > input:nth-child(1):checked ~ div.content > div:nth-of-type(1) {
display: block;
border-top: none;
background-image:url(picture-1.jpg);
min-height: 380px;
background-repeat:no-repeat;
-webkit-background-size: contain;
-moz-background-size: contain;
background-size: contain;
}
.tabs_accordion > input:nth-child(2):checked ~ ul.tabs > li:nth-of-type(2) {
font-weight: bold;
}
.tabs_accordion > input:nth-child(2):checked ~ div.content > label:nth-of-type(2) {
font-weight: bold;
margin-bottom: 0;
}
.tabs_accordion > input:nth-child(2):checked ~ div.content > div:nth-of-type(2) {
display: block;
border-top: none;
background-image:url(picture-2.jpg);
min-height: 380px;
background-repeat:no-repeat;
-webkit-background-size: contain;
-moz-background-size: contain;
background-size: contain;
}
.tabs_accordion > input:nth-child(3):checked ~ ul.tabs > li:nth-of-type(3) {
font-weight: bold;
}
.tabs_accordion > input:nth-child(3):checked ~ div.content > label:nth-of-type(3) {
font-weight: bold;
margin-bottom: 0;
}
.tabs_accordion > input:nth-child(3):checked ~ div.content > div:nth-of-type(3) {
display: block;
border-top: none;
background-image:url(picture-3.jpg);
min-height: 380px;
background-repeat:no-repeat;
-webkit-background-size: contain;
-moz-background-size: contain;
background-size: contain;
}



.tabs_accordion ul.tabs {
display: table;
width: 100%;
display: none;
background-color: #deeab4;
padding: 0;
margin: 0;
overflow: hidden;
box-sizing: border-box;
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
color:#333333;
font-size: 16px;
}
.tabs_accordion ul.tabs li {
display: table-cell;
cursor: pointer;
width: 238px;

}

.tabs_accordion ul.tabs li label:active {
background-color: #8cd600;

}


/* begin controls hover */
.tabs_accordion [id^="tab"]:checked + label {
background: green;
}
/* end controls hover */

.tabs_accordion ul.tabs li label {
display: block;
height: 74px;
padding: 20px;
text-align: center;
border-left: 2px solid #c8c8c8;
margin: 0px 0;
box-sizing: border-box;
cursor: pointer;
font-weight: bold;
}
.tabs_accordion ul.tabs li label:focus {
background-color: #8cd600;
}
.tabs_accordion ul.tabs li label:active {
background-color: #8cd600;
}
.tabs_accordion ul.tabs li:hover {
background-color: #8cd600;
color:#FFFFFF;
}

.tabs_accordion ul.tabs li:hover label {
border-left-color: transparent;
}
.tabs_accordion ul.tabs li:hover + li > label {
border-left-color: transparent;
}
.tabs_accordion ul.tabs li:first-child label {
border-left-color: transparent;
}
.tabs_accordion div.content > label {
display: block;
background-color: #deeab4;
padding: 20px;
margin-bottom: 1em;
cursor: pointer;
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
text-align:center;
}
.tabs_accordion div.content > div {
display: none;
padding: 10px;

margin-bottom: 1em;
}

.tabs_accordion ul.tabs {
display: table;
}
.tabs_accordion div.content > label {
display: none;
}
.tabs_accordion div.content > div {
margin-bottom: 0;
}
<div class="tabs_accordion">

<input type="radio" name="tabs" value="tab_2" id="tab_2_content_control" tabindex="0" />
<input type="radio" name="tabs" value="tab_1" id="tab_1_content_control" checked="checked" tabindex="0" />
<input type="radio" name="tabs" value="tab_3" id="tab_3_content_control" tabindex="0" />

<ul class="tabs">
<li><label for="tab_1_content_control">Option 1</label></li>
<li><label for="tab_2_content_control">Option 2</label></li>
<li><label for="tab_3_content_control">Option 3</label></li>
</ul>

<div class="content">

<label for="tab_1_content_control">Option 1</label>
<div class="tab_1_content">

</div>

<label for="tab_2_content_control">Option 2</label>
<div class="tab_2_content">
</div>

<label for="tab_3_content_control">Option 3</label>
<div class="tab_3_content">
</div>

</div>
</div>

最佳答案

为事件标签添加一个新的CSS类

.activeTab
{
background-color: green;
}

在您的文档就绪事件中,您可以获得第一个选项卡项(li 元素),然后将这个新的 css 类添加到其中。此外,当用户点击时,删除当前事件类并将 activeTab 类设置为新点击的元素。

$(function(){  

//Make first one active
$(".tabs>li").first().addClass("activeTab");

$(".tabs>li").click(function(e){

var _this = $(this);

//Remove existing active one
$(".activeTab").removeClass("activeTab");

//Set the css class to the clicked one
_this.addClass("activeTab");

});


})

Here是一个工作样本。

关于javascript - 在选项卡上使用 Javascript 的事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138849/

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