写一个tabs 里面有全选 复选,所有的tab页面相同的复选是联动的,每个页面的全选也与复选联动
时间: 2023-05-23 20:07:36 浏览: 131
页面中全选
这里是一个基本的实现,可以根据具体的需求进行调整:
HTML部分:
```
<!-- tabs -->
<div class="tabs">
<button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button>
<button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button>
<button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button>
</div>
<!-- tab页面 -->
<div class="tabcontent" id="tab1">
<div class="checkAll">
<input type="checkbox" class="checkAllBox" onclick="checkAll('tab1')">全选
</div>
<div class="checkBoxes">
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab1')" id="checkbox1-tab1">
<label for="checkbox1-tab1">选项1</label>
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab1')" id="checkbox2-tab1">
<label for="checkbox2-tab1">选项2</label>
</div>
</div>
<div class="tabcontent" id="tab2">
<div class="checkAll">
<input type="checkbox" class="checkAllBox" onclick="checkAll('tab2')">全选
</div>
<div class="checkBoxes">
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab2')" id="checkbox1-tab2">
<label for="checkbox1-tab2">选项1</label>
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab2')" id="checkbox2-tab2">
<label for="checkbox2-tab2">选项2</label>
</div>
</div>
<div class="tabcontent" id="tab3">
<div class="checkAll">
<input type="checkbox" class="checkAllBox" onclick="checkAll('tab3')">全选
</div>
<div class="checkBoxes">
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab3')" id="checkbox1-tab3">
<label for="checkbox1-tab3">选项1</label>
<input type="checkbox" class="checkBox" onchange="checkBoxChange('tab3')" id="checkbox2-tab3">
<label for="checkbox2-tab3">选项2</label>
</div>
</div>
```
JavaScript部分:
```
// 打开默认tab
document.getElementById("tab1").style.display = "block";
// 切换tab
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
// 全选
function checkAll(tabName) {
var checkboxes = document.getElementById(tabName).getElementsByClassName("checkBox");
var checkAllBox = document.getElementById(tabName).getElementsByClassName("checkAllBox")[0];
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = checkAllBox.checked;
}
}
// 复选框改变
function checkBoxChange(tabName) {
var checkboxes = document.getElementById(tabName).getElementsByClassName("checkBox");
var checkAllBox = document.getElementById(tabName).getElementsByClassName("checkAllBox")[0];
var allChecked = true;
for (var i = 0; i < checkboxes.length; i++) {
if (!checkboxes[i].checked) {
allChecked = false;
break;
}
}
checkAllBox.checked = allChecked;
// 如果所有的tab都选中同一个复选框,则联动
var allTabs = document.getElementsByClassName("tabcontent");
for (var j = 0; j < checkboxes.length; j++) {
var multiTabChecked = true;
for (var k = 0; k < allTabs.length; k++) {
var tabCheckboxes = allTabs[k].getElementsByClassName("checkBox");
var thisTabChecked = false;
for (var l = 0; l < tabCheckboxes.length; l++) {
if (checkboxes[j] === tabCheckboxes[l] && checkboxes[j].checked) {
thisTabChecked = true;
break;
}
}
if (!thisTabChecked) {
multiTabChecked = false;
break;
}
}
if (multiTabChecked) {
checkboxes[j].checked = true;
}
}
}
```
阅读全文