[英]Javascript check if div element is empty
I have variable which hold div element.我有保存div 元素的变量。 In Javascript I want to check, how to validate if div element holds any child nodes or is emtpy?
在我想检查的 Javascript 中,如何验证 div 元素是否包含任何子节点或是否为空? The response can be in TRUE or FALSE
响应可以是 TRUE 或 FALSE
I have tried with my options from google but not getting exact results for javascript.我曾尝试使用 google 的选项,但没有获得 javascript 的确切结果。 please suggest me if anyone has better input.
如果有人有更好的输入,请建议我。
NOTE: Validation has to be done in Javascript not in Jquery and div doesn't have any ID here..注意:验证必须在 Javascript 中而不是在 Jquery 中完成,div 在这里没有任何 ID..
var ele = "<div class='....'></div>"
Your code here shows a string variable, not a div element.您在此处的代码显示了一个字符串变量,而不是一个 div 元素。
Otherwise simply get your div element using (for example)否则只需使用(例如)获取您的 div 元素
var myDiv = document.getElementById("divId");
And then simply check if it's content is empty or not :然后简单地检查它的内容是否为空:
if (myDiv.innerHTML == "")
{
//myDiv is empty
}
else
{
//myDiv isn't empty
}
The trick is putting your string inside a $() selector.诀窍是将您的字符串放在 $() 选择器中。 Then you can use css selectors etc for checking if it is empty:
然后你可以使用 css 选择器等来检查它是否为空:
var ele1 = "<div class='myclass'></div>";
if ($(ele1).is(':empty')) {
console.log('1 is empty!');
} else {
console.log('1 is not empty!');
}
var ele2 = "<div class='myclass'>Duh, not empty at all!</div>";
if ($(ele2).is(':empty')) {
console.log('2 is empty!');
} else {
console.log('2 is not empty!');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.