删除表中的特定列
Remove the specific column in table
本文关键字:删除 更新时间:2023-09-26
我正在努力解决表列隐藏和显示使用复选框的问题。 我想删除火星列(粗体)和相应的数据(粗体)。
删除火星列后,金星及其受人尊敬的数据值将在表中居中。
在这里查看: http://jsfiddle.net/bL44n3aj/3/
删除火星列后,我想要这个输出:http://jsfiddle.net/2Lcsc2go/
我的 CSS 和 HTML 部分:
td{
border:1px solid #000;
}
th{
border:1px solid red;
font-weight:normal !important;
}
tr.sub-header th{
text-align:center !important;
border:1px solid blue !important;
font-weight:bold !important;
}
tr.sub-header-value td{
text-align:center !important;
font-weight:bold !important;
}
<table width="80%">
<tr>
<th>Produced</th>
<th>Sold</th>
<th>Produced</th>
<th>Sold</th>
</tr>
<tr class="sub-header">
<th colspan="2">Mars</th>
<th colspan="2" scope="colgroup">Venus</th>
</tr>
<tr>
<td>50,000</td>
<td>30,000</td>
<td>100,000</td>
<td>80,000</td>
</tr>
<tr class="sub-header-value">
<td colspan="2">Mars data</td>
<td colspan="2"> Venus data </td>
</tr>
<tr>
<td>10,000</td>
<td>5,000</td>
<td>12,000</td>
<td>9,000</td>
</tr>
<tr class="sub-header-value">
<td colspan="2">Mars data</td>
<td colspan="2">Venus data</td>
</tr>
</table>
<input type="checkbox"> Remove Mars
这是一种笨拙的方式。为了不那么笨拙,您需要向所有 tds 添加类。
我正在使用jQuery,你想要一个JavaScript版本吗?
$(function(){
var removed = false;
$("#removeMars").click(function(){
if (!removed) {
$.each($("#t tr"), function(){
var tds = $(this).find("th, td");
if (tds.length == 2) {
$(tds[1]).attr("colspan", "4");
$(tds[0]).remove();
}
})
removed = true;
}
})
})
td{
border:1px solid #000;
}
th{
border:1px solid red;
}
tr.sub-header th{
text-align:center !important;
border:1px solid blue !important;
font-weight:bold !important;
}
tr.sub-header-value td{
text-align:center !important;
font-weight:bold !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="t" width="80%">
<tr>
<th>Produced</th>
<th>Sold</th>
<th>Produced</th>
<th>Sold</th>
</tr>
<tr class="sub-header">
<th colspan="2" >Mars</th>
<th colspan="2" scope="colgroup">Venus</th>
</tr>
<tr>
<td>50,000</td>
<td>30,000</td>
<td>100,000</td>
<td>80,000</td>
</tr>
<tr class="sub-header-value">
<td colspan="2">Mars data</td>
<td colspan="2"> Venus data </td>
</tr>
<tr>
<td>10,000</td>
<td>5,000</td>
<td>12,000</td>
<td>9,000</td>
</tr>
<tr class="sub-header-value">
<td colspan="2">Mars data</td>
<td colspan="2">Venus data</td>
</tr>
</table>
<input type="checkbox" id="removeMars"> Remove Mars
完成的解决方案:
$(function(){
var removed = false;
$("#removeMars").change(function(){
if(this.checked) {
if (!removed) {
$.each($("#t tr"), function(){
var tds = $(this).find("th, td");
if (tds.length == 2) {
$(tds[1]).attr("colspan", "4");
$(tds[0]).hide();
}
})
removed = true;
}
}
else{
$.each($("#t tr"), function(){
var tds = $(this).find("th, td");
if (tds.length == 2) {
$(tds[1]).attr("colspan", "2");
$(tds[0]).show();
}
})
removed = false;
}
})
})
检查这个小提琴链接:http://jsfiddle.net/qLo60ux8/
相关文章:
- 使用php或javascript从facebook相册URL中删除多余的部分
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 如何删除多行HTML排列中的空白
- 如何从rails中的代码中删除新行( )
- 删除对HTML元素的拖动
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 按顺序添加和删除类
- iframe正在添加标签,需要删除它们
- 如何在HTML输入字段中添加不可删除的后缀
- addData()从最新图表中删除.js 2.1.3-怎么了
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 删除CKEditor工具栏按钮,但不删除功能
- 当浏览器上的后退按钮到达主页时,我需要删除Class
- 正在删除node.js中已验证的网站
- 正在从页面中删除iframe
- 如何从 HTML 查询中删除项目
- 如何在iOS应用程序中从Firebase中删除超过6小时的数据
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 在不知道深度或父属性的情况下从对象中删除属性
- 解析JSON并从中删除对象会出错