admin管理员组文章数量:1432602
How can it be tested using jQuery or JavaScript, if a given element has only one child element (including text nodes, but ignoring blank-spaces).
Example: for element 'div' in markup below, the test should fail because it has two child elements: p and the text node:
<div>
<p>Test-1</p> - subline
</div>
Example: for element 'div' in markup below, the test should pass because it has only one child element: p (although spaces are ignored):
<div>
<p>Test-1</p>
</div>
It seems like element.children() won't work because it ignores text nodes. element.contents() may work, but it doesn't ignore blank-spaces.
How can it be tested using jQuery or JavaScript, if a given element has only one child element (including text nodes, but ignoring blank-spaces).
Example: for element 'div' in markup below, the test should fail because it has two child elements: p and the text node:
<div>
<p>Test-1</p> - subline
</div>
Example: for element 'div' in markup below, the test should pass because it has only one child element: p (although spaces are ignored):
<div>
<p>Test-1</p>
</div>
It seems like element.children() won't work because it ignores text nodes. element.contents() may work, but it doesn't ignore blank-spaces.
Share Improve this question edited Mar 7, 2016 at 8:23 Nick asked Mar 7, 2016 at 8:20 NickNick 1,4242 gold badges19 silver badges41 bronze badges 04 Answers
Reset to default 3You will have to use a custom filter
var elements = $('div');
elements.each(function() {
var element = $(this);
var count = element.contents().filter(function() {
return this.nodeType == Node.ELEMENT_NODE || (this.nodeType == Node.TEXT_NODE && !!$.trim(this.nodeValue))
}).length;
snippet.log(this.id + ': ' + count)
});
<!-- Provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="1">
<p>Test-1</p>
</div>
<div id="2">
<p>Test-1</p>1
</div>
<div id="3">
<p>Test-1</p><span>x</span>
</div>
<div id="4">
2
<p>Test-1</p>3
</div>
you can try simple js as
element[0].childNodes.length
this will include the text nodes as well as the normal child nodes.
if a given element has only one child element (including text nodes, but ignoring blank-spaces).
To exclude whitespaces
element.contents().filter(function() {
return this.nodeType === 3 && this.nodeValue.trim().length > 0;
}).length; //to get the number of text nodes which are not white-spaces
or in pure js
element[0].childNodes.filter(function() {
return this.nodeType === 3 && this.nodeValue.trim().length > 0;
}).length;
Non jquery version.
Could be optimized, but this works. value.nodeType === 1
to check if it's an element, and we increase the counter.
var count = 0;
Array.prototype.slice.call(document.getElementById('parent_id_here').childNodes, 0).forEach(function (value) {
if (value.nodeType === 1) {
count++;
}
});
Demo
You can use this condition:
$('div').html().trim() == $('div > *:first-child')[0].outerHTML.trim()
本文标签: javascriptCheck if element has only one childStack Overflow
版权声明:本文标题:javascript - Check if element has only one child - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745607781a2665936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论