[英]javascript to hide a div if empty
I am looking for javascript (not jquery as client has specified) to hide a div with class=top, if the div has no content. 我正在寻找javascript(如果客户端已指定,则不是jquery)来隐藏class = top的div(如果div没有内容)。 I can do it using jquery like below, but need to use javascript. 我可以使用如下所示的jquery来做到这一点,但需要使用javascript。 Any ideas please? 有什么想法吗?
$('div.top:empty').hide();
Something like: 就像是:
var top = document.getElementsByClassName("top");
for (var i = 0; i < top.length; i++) {
if (top[i].innerHTML.length == 0)
top[i].style.display = "none";
}
you could use innerHTML
property to check if the selected div.top
element contains content. 您可以使用innerHTML
属性来检查所选的div.top
元素是否包含内容。 something like this. 这样的事情。
var topDiv = document.getElementsByClassName('top')[0];
if(topDiv.innerHTML === '') {
topDiv.style.display = 'none';
}
(if(document.getElementById("yourDiv").innerHTML=="")
{
document.getElementById("yourDiv").style.display='none';
}
Use the following script: 使用以下脚本:
var divContent = $('div .top')[0].innerHTML;
if (divContent === '')
{
$('div .top').hide();
}
You need to give id to DIV which you want to hide As there are no function in javascript by which you can find div by class. 您需要为要隐藏的DIV提供ID,因为javascript中没有可以按类查找div的函数。 HTML: HTML:
<div class="top" id="divId"></div>
Javascript: Javascript:
if( document.getElementById("divId").innerHTML == "" )
{
document.getElementById("divId").style.display='none';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.