简体   繁体   English

使用javascript隐藏表格中的表格行元素

[英]Hide a table row element inside a table using javascript

I want to hide a table row when a td has no value, but my javascript function is failing. 当td没有值时,我想隐藏表格行,但是我的javascript函数失败。 It keeps going on the "else". 它一直在“其他”。

HTML HTML

<table>
   <tr id="locOfWorkRow"> 
     <td><span>Location of Work</span></td>
   </tr>
   <tr>
     <td><span>This is the label</span></td>
     <td id="road"><span></span></td>
   </tr>
</table>

Javascript 使用Javascript

var r = document.getElementById('road').innerHTML; 

 if(r == ""){ alert("on if");
   document.getElementById('locOfWorkRow').style.display = "none";
 }else{
    alert("on else");
 }

Instead of checking for an empty string, check whether the length of the innerHTML is more than 0 instead. 而不是检查空字符串,而是检查innerHTML的长度是否大于0。

r.length > 0

If the string is empty, the length will return 0, if the the innerHTML contains any characters the length will return a value greater than 0. 如果字符串为空,则长度将返回0,如果innerHTML包含任何字符,则长度将返回大于0的值。

What you are currently doing with r == "" is checking whether r is empty and that is why the "on if" alert is showing. 您当前正在使用r == ""来检查r是否为空,这就是为什么显示“ on if”警报的原因。

See this example 看这个例子

http://jsfiddle.net/9ny6598u/ http://jsfiddle.net/9ny6598u/

var r = document.getElementById('road').innerHTML;

if (r.length > 0) {
    alert("on if");
    document.getElementById('locOfWorkRow').style.display = "none";
} else {
    alert("on else");
}

DEMO DEMO

You are using wrong if condition 您在以下情况下使用错误

use if(r != "") instead of r=="" 使用if(r!=“”)代替r ==“”

Actually your if condition is wrong 其实你的条件是否错误

Example : http://jsfiddle.net/kevalbhatt18/bvop1gdq/ 范例http//jsfiddle.net/kevalbhatt18/bvop1gdq/

 if(r !== ""){ alert("on if"); document.getElementById('locOfWorkRow').style.display = "none"; }else{ alert("on else"); } 

Or 要么

 if (r.length > 0) { alert("on if"); document.getElementById('locOfWorkRow').style.display = "none"; } else { alert("on else"); } 

Hope you are not loading your javascript before page try to write your javascript at the end of the page so the element is available and you not get the null reference. 希望您不要在页面之前加载javascript,然后尝试在页面末尾编写javascript,以便该元素可用并且您不会获取null引用。

and by innerhtml you got the "span Road 1 /span" so write condition as per that. 通过innerhtml,您获得了“ span Road 1 / span”,因此请按照以下条件编写条件。 and its work fine if you load your script below body. 如果将脚本加载到主体下面,它的工作效果很好。

Make sure you have loaded your script after html load. 确保在html加载后加载了脚本。

using undefined as comparison instead of empty string and also use === instead of == 使用undefined作为比较而不是空字符串,并且还使用===代替==

if(r === undefined){ alert("on if");
  document.getElementById('locOfWorkRow').style.display = "none";
}else{
  alert("on else");
}

可以使用innerText或textContent代替innerHTML

var r = document.getElementById('road').innerText || document.getElementById('road').textContent; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM