gpt4 book ai didi

javascript - 创建选项卡,每个选项卡都有两个文本区域

转载 作者:行者123 更新时间:2023-12-03 07:12:38 25 4
gpt4 key购买 nike

我想在 HTML 中创建两个选项卡,单击每个选项卡时它应该显示两个文本区域(空),我已经尝试过并能够创建,但我的逻辑涉及 for 循环来显示基于的文本区域我的应用程序不支持选择选项卡,现在我想要一个简单的逻辑来创建和显示文本区域

我的JS代码:

function tabs(selectedtab) {    
// contents
var s_tab_content = "tab_content_" + selectedtab;
//alert(s_tab_content);
var contents = document.getElementsByTagName("div");
for(var x=0; x<contents.length; x++) {
name = contents[x].getAttribute("name");
if (name == 'tab_content') {
if (contents[x].id == s_tab_content) {
contents[x].style.display = "block";
} else {
contents[x].style.display = "none";
}
}
}
}

HTML 代码:

    <html>
<body>
<div id="tab1" name="tab" style="cursor: pointer; cursor: hand; width:90px;
float: left;
height: 22px ;
margin-top: 0px;
background-color:lightgrey;
font-size: 13px;
text-align: center;
border-bottom: none;
display: table-cell;"
onClick="tabs(1)">
<div style="margin-top: 3px" >Tab1</div>
</div>
<div id="tab2" name="tab" style="cursor: pointer; cursor: hand; width:90px;background-color:lightgrey;float: left;height: 20px; margin-top: 2px; font-size: 12px;text-align: center" onClick="tabs(2)">
<div style="margin-top: 3px">Tab2</div>
</div>
<div style="width:auto;height: 22px; border-bottom-color: white;border-bottom-style: solid;border-bottom-width: 1px"></div>



<div name="tab_content" id="tab_content_1" style="display: block; margin-left:20px;" >
<br></br>
<br></br>
<div id="legendReport">
Notes 1:
</div>
<textarea id="reportNotes_textArea" name="textArea1" rows="4" value="" cols="50"></textarea>
<br></br>
<br></br>
<div id="legendRough">
Notes 2:
</div>
<textarea id="roughNotes_textArea" name="textArea2" rows="4" cols="50"></textarea>
</div>

<div name="tab_content" id="tab_content_2" class="tab_content" style="display: none;margin-left:20px;">
<br></br>
<br></br>
<textarea id="geometry_first_textArea" name="textArea3" rows="4" cols="50" ></textarea>
<br></br>
<br></br>
<textarea id="geometry_second_textArea" name="textArea4" rows="4" cols="50"></textarea>
<br></br>
<br></br>
<br></br>
<br></br>
<div id="legendRough">
Schematic:
</div>

<img id="myImage" src="" style="width:304px;height:228px;"></img>

</div>

</body>
</html>

最佳答案

首先:您正在使用 name属性来选择<div>元素。根据spec , div 不允许具有此属性。使用class属性可能更适合您的需求。

<div class="tab_content" id="tab_content_1">...</div>
<div class="tab_content" id="tab_content_2">...</div>

其次:如果您将拥有超过 2 个选项卡,您最终将需要某种循环。当显示新元素时,您必须能够隐藏其他元素。

如果像您的示例一样,只有 2 个选项卡,您可以使用 document.querySelector像这样:

function tabs(selectedtab) {    
var s_tab_content = "#tab_content_" + selectedtab;
var tabToShow = document.querySelector(".tab_content" + s_tab_content);
var tabToHide = document.querySelector(".tab_content:not(" + s_tab_content + ")");

// Add your code to hide and show the tabs here:
// ...
}

第一个选择器选择第一个元素,该元素同时具有class“tab_content”和id您已存储在 s_tab_content 。第二个选择器选择第一个具有“tab_content”类但没有 id 的元素。

但是,我的建议是保持循环并像这样解决您的问题:

  1. 全选tab_content div
  2. 将它们全部隐藏
  3. 使用document.getElementById找到要显示的一个
  4. 显示您需要的内容

即使您添加许多选项卡,这也会继续工作。

关于javascript - 创建选项卡,每个选项卡都有两个文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567923/

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