gpt4 book ai didi

javascript - CSS JS 选项卡 - 如果单击另一个选项卡,则不隐藏当前内容,

转载 作者:行者123 更新时间:2023-11-28 01:26:31 24 4
gpt4 key购买 nike

我的这个标签主要是用 CSS 制作的,但我使用 CMS 动态构建它,在这种情况下,CSS 不足以隐藏/显示所有元素,所以我制作了一个 JS。

它在被选中后显示内容选项卡效果很好,但它不会隐藏当前内容以显示新内容,因此它只是将新选择的内容添加到已存在的内容下

请随意运行该代码段以查看它的工作原理,我不知道我是否将它弄得太复杂了,但我们非常欢迎您提供任何建议和帮助。

谢谢!

/*this code is to identify the checked tab and display the content*/
var getAllDataInput = document.querySelectorAll('[data-input]');
for (let i = 0, len = getAllDataInput.length; i < len; i++) {

let getSingleDataInput = getAllDataInput[i];
let tabSelected = getAllDataInput[i].defaultChecked;
if (tabSelected === true) {
var DataInputValue = getSingleDataInput.dataset.input;
var findDataSetMatch = document.querySelector('[data-section="' + DataInputValue + '"]');
findDataSetMatch.style.display = "block";
}
}

/*this is the function that is called on click, it will find the Tab the related Content and display it */
function hideTab(tabNumber) {
let lastSelected = null;
let checkTab = document.querySelector('[data-input="' + tabNumber + '"]');
let contentTab = document.querySelector('[data-section="' + tabNumber + '"]');

if (checkTab.checked === true) {
contentTab.style.display = "block";
}

/*If another tab is selected I need to hide the current content and display the new one!*/
}
    .tabsWrapper {
margin: 5rem 1rem;
display: inline-block;
width: 100%;
}
.tab-content {
display: none;
padding: 20px 0 0;
border-top: 1px solid #ddd;
}
.tab-title {
display: none;
}
.tab-label {
display: inline-block;
margin: 0 0 -1px;
padding: 15px 25px;
font-weight: 600;
text-align: center;
color: #bbb;
border: 1px solid transparent;
}
.tab-label:hover {
color: #888;
cursor: pointer;
}
.tab-title:checked + label {
color: #555;
border: 1px solid #ddd;
border-top: 2px solid orange;
border-bottom: 1px solid #fff;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>

<div class="tabsWrapper">
/*FIRST TABS*/
<input id="tab11" type="radio" name="tabs1" class="tab-title" data-input="11" onClick="hideTab(11)" checked>
<label for="tab11" class="tab-label">Title A</label>

<input id="tab12" type="radio" name="tabs1" class="tab-title" data-input="12" onClick="hideTab(12)">
<label for="tab12" class="tab-label">Title B</label>

/*FIRST CONTENT*/
<section id="ctab11" class="tab-content" data-section="11">
<p>Lorem ipsum dolor sit amet.</p>
</section>
<section id="ctab12" class="tab-content" data-section="12">
<p>Invidunt menandri duo at, at everti prompta eos.</p>
</section>

</div>

<div class="tabsWrapper">
/*SECOND TABS*/
<input id="tab21" type="radio" name="tabs2" class="tab-title" data-input="21" onClick="hideTab(21)" checked>
<label for="tab21" class="tab-label">Title A</label>

<input id="tab22" type="radio" name="tabs2" class="tab-title" data-input="22" onClick="hideTab(22)">
<label for="tab22" class="tab-label">Title B</label>

/*SECOND CONTENT*/
<section id="ctab21" class="tab-content" data-section="21">
<p>Lorem ipsum dolor sit amet.</p>
</section>
<section id="ctab22" class="tab-content" data-section="22">
<p>Invidunt menandri duo at, at everti prompta eos.</p>
</section>
</div>

</body>
</html>

最佳答案

您只需将一个 onClick 事件添加到输入标记以将其连接到您的 js 函数。然后写一个Js函数,改变你想改变的那个标签的style属性。

<style>
#ctab21 {
display: block;
color: blue;
}
</style>

<body>
<div>Hide text:</div>

<!-- onclick attribute connects hideTab function to input tag -->
<input id="tab21" type="checkbox" onClick="hideTab()">

<section>

<!-- hidden text to be displayed -->
<p id="ctab21">This text will become hidden</p>

</section>
</body>

<script>
function hideTab() {

// get the checkbox; id ="tab21"
let checkBox = document.getElementById("tab21");

// get the p tag; id ="ctab21"
let text = document.getElementById("ctab21");

// If the checkbox is checked, hide the text
if (checkBox.checked == false) {
text.style.display = "block";
} else {
text.style.display = "none";
}
}
</script>

引用:https://www.w3schools.com/howto/howto_js_display_checkbox_text.asp

关于javascript - CSS JS 选项卡 - 如果单击另一个选项卡,则不隐藏当前内容,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51216512/

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