使用 jQuery 显示/隐藏表头
Show/Hide table header using jQuery
基本上,在循环遍历每个项目时,我根据表的内容有两种不同类型的标题:
JS代码:
if (idLayer == "nss") {
var tabs = [];
for (var layerId in layers) {
var results = layers[layerId];
idResults[idLayer + layerId] = results;
var useHeaderComm = false;
var count = results.length;
var label = "", content = "";
var resultsId = idLayer + layerId;
switch (layerId) {
case "2":
label = "New Sale Sites(" + count + ")";
if (count == 0) break;
content += "<table><tr id='headerComm'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Allowable GFA</th><th>Office GFA</th><th>Retail GFA</th><th>Other GFA</th></tr>";
content += "<table><tr id='headerResi'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Estimated DU</th></tr>";
for (var j = 0; j < count; j++) {
if (latlng !== 'Null') {
var attributes = results[j].feature.attributes;
var resultsId = idLayer + layerId;
if(attributes["LAND_USE"] == "COMM" || attributes["LAND_USE"] == "OFF" )
{
content += "<tr>";
content += "<td><a href='javascript:void(0)' onclick='showFeature('"" + resultsId + "'"," + j + ")'>" + attributes["LOCATION"] + "</a></td>";
content += "<td>" + attributes["PLANNING_AREA"] + "</td>";
content += "<td>" + attributes["LAND_USE"] + "</td>";
if(attributes["GPR"] != "Null" && attributes["GPR"] != "" && attributes["GPR"] != undefined && attributes["GPR"] != "null")
{
content += "<td>" + attributes["GPR"] + "</td>";
}
else
{
content += "<td>N.A</td>";
}
content += "<td>" + attributes["SITE_AREA"] + "</td>";
content += "<td>" + attributes["ALLOWABLE_GFA"] + "</td>";
content += "<td>" + attributes["OFFICE_GFA"] + "</td>";
content += "<td>" + attributes["RETAIL_GFA"] + "</td>";
content += "<td>" + attributes["HOTEL_GFA"] + "</td>";
content += "</tr>";
useHeaderComm = true;
}else
{
content += "<tr>";
content += "<td><a href='javascript:void(0)' onclick='showFeature('"" + resultsId + "'"," + j + ")'>" + attributes["LOCATION"] + "</a></td>";
content += "<td>" + attributes["PLANNING_AREA"] + "</td>";
content += "<td>" + attributes["LAND_USE"] + "</td>";
if(attributes["GPR"] != "Null" && attributes["GPR"] != "" && attributes["GPR"] != undefined && attributes["GPR"] != "null")
{
content += "<td>" + attributes["GPR"] + "</td>";
}
else
{
content += "<td>N.A</td>";
}
content += "<td>" + attributes["SITE_AREA"] + "</td>";
if(attributes["ESTIMATED_DU"] != "Null" && attributes["ESTIMATED_DU"] != "" && attributes["ESTIMATED_DU"] != undefined && attributes["ESTIMATED_DU"] != "null")
{
content += "<td>" + attributes["ESTIMATED_DU"] + "</td>";
}
else
{
content += "<td>N.A</td>";
}
content += "</tr>";
useHeaderComm = false;
}
}
if(useHeaderComm == true)
{
$('.headerResi').hide();
}else
{
$('.headerComm').hide();
}
}
content += "</table>";
break;
}
}
}
我正在尝试做的是遍历结果中的每个项目,并根据某些属性显示表标题。
例如,如果LAND_USE
"COMM"
或"OFF"
则使用的表头应使用 headerComm
的 id tr
。
但是,对于这些代码,两个标题都只是显示而不隐藏或显示。我想知道为什么会这样。
我认为你正在使用id作为你的<tr>
所以,你的代码应该是,
if(useHeaderComm == true)
{
$('#headerResi').hide();
}
else
{
$('#headerComm').hide();
}
还有一件事。您无需在此代码中再添加两次<table>
,
content += "<table><tr id='headerComm'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Allowable GFA</th><th>Office GFA</th><th>Retail GFA</th><th>Other GFA</th></tr>";
content += "<table><tr id='headerResi'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Estimated DU</th></tr>";
应该是,
content += "<table><tr id='headerComm'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Allowable GFA</th><th>Office GFA</th><th>Retail GFA</th><th>Other GFA</th></tr>";
content += "<tr id='headerResi'><th>Location</th><th>Planning Area</th><th>Development Type</th><th>GPR</th><th>Site Area</th><th>Estimated DU</th></tr>";
这是因为当你做你的条件时,元素不会附加到 DOM 中。 jQuery 仅在 DOM 或创建的元素上进行搜索。
我建议在将其附加到 DOM 后检查要显示哪 1 个:
$(selector).append(content);
if(useHeaderComm == true)
{
$('.headerResi').hide();
}else
{
$('.headerComm').hide();
}
相关文章:
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 显示5秒后隐藏潜水
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 浮动页脚栏-使用Bootstrap隐藏
- 在jquery中为显示/隐藏设置cookie
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 从var向代码隐藏函数传递值
- Image赢得't隐藏在滚动jQuery上
- 具有rowGrouping的数据表无法隐藏列
- Jquery隐藏未触发
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 分部隐藏在jquery中不起作用
- 点击后隐藏潜水?(但如果Div是一面旗帜呢?)
- javascript.点击隐藏事件故障
- 禁用(而不是隐藏)浏览器滚动条
- PHP Javascript显示/隐藏按钮不工作
- 如果没有行,则隐藏表头
- 使用jquery从表头隐藏图像
- 使用 jQuery 显示/隐藏表头