admin管理员组文章数量:1401653
I have a div, which contains n number of child nodes. I have a for loop:
for (iloop = 0; iloop < summaryDiv.childNodes.length; iloop++) {
if (summaryDiv.childNodes[iloop].innerHTML.contains('display:block')) {
flag = false;
}
}
Here some of the child nodes don't contain the element innerHTML. I need to check if the child node contains innerHTML element, then I need to check for the style (display:block). How can I do this?
I have a div, which contains n number of child nodes. I have a for loop:
for (iloop = 0; iloop < summaryDiv.childNodes.length; iloop++) {
if (summaryDiv.childNodes[iloop].innerHTML.contains('display:block')) {
flag = false;
}
}
Here some of the child nodes don't contain the element innerHTML. I need to check if the child node contains innerHTML element, then I need to check for the style (display:block). How can I do this?
Share Improve this question edited Jan 12, 2010 at 6:39 Steve Harrison 126k17 gold badges89 silver badges72 bronze badges asked Jan 12, 2010 at 5:06 niminimi 5,50718 gold badges62 silver badges90 bronze badges 1-
Changed all occurrences of
innerHtml
in question toinnerHTML
—JavaScript is case-sensitive. – Steve Harrison Commented Jan 12, 2010 at 6:42
3 Answers
Reset to default 5If you want to check the style of something, you do not need innerHTML
. Instead, access the display
value directly from the style
element that is defined by default on DOM elements.
var children = summaryDiv.childNodes; // Avoid excessive scope chain walking (faster!)
for ( var i=0, l=children.length; i<l; i++ ) {
if ( children[i].style && children[i].style.display == "block" ) {
flag = false;
}
}
Use the display property directly:
for (var i = 0; i < summaryDiv.childNodes.length; i++) {
if (summaryDiv.childNodes[i].style && summaryDiv.childNodes[i].style.display == 'block') {
flag = false;
}
}
You can check if the element has a property "innerHTML".
<html>
<body>
<div id="div">hello</div>
<script type="text/javascript">
document.write("div: " + ("innerHTML" in document.getElementById("div")));
document.write("<br />");
document.write("div.child: " + ("innerHTML" in document.getElementById("div").firstChild));
</script>
</body>
</html>
produces:
div: true
div.child: false
本文标签: javascriptHow to check if a div has element innerHTMLStack Overflow
版权声明:本文标题:javascript - How to check if a div has element innerHTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744311912a2600069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论