开发者

MooTools: getChildren() INCLUDING text nodes?

I'd like to get all the children of an element, including text nodes. How c开发者_运维知识库an I do this in MooTools? The documentation at mootools.net explicitly says that getChildren() excludes text nodes.


You could use the standard childNodes DOM property, which works in all the major desktop browsers:

var el = document.getElementById("someElement");
var children = el.childNodes;
for (var i = 0, len = children.length; i < len; ++i) {
    alert( "Is text node: " + (children[i].nodeType == 3) );
}

Note that childNodes is not an array and therefore doesn't have Array's methods, but has a length property and allows you to access its members via numerical properties. Also, IE does not include whitespace text nodes whereas other browsers do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜