jQuery不支持选择顶级nodetext? [英] select top level nodetext is not supported with jQuery?

查看:81
本文介绍了jQuery不支持选择顶级nodetext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我需要顶级文本节点时,我必须写下这个长篇故事吗?

Whenever I needed the top level text node I must write this long story?

$("#hello").clone().children().remove().end().text();

为什么没有支持它的原生函数?

Why is there no native function to support it ?

推荐答案

这是获取文本节点的一种非常好的方法..

this is a very good method to get text nodes..

getTextNodes: function( el ) {
        var nodes = [];
        if(el instanceof NodeList || el instanceof HTMLCollection ){ 
                    //perhaps a better test for an array/collection of objects is required here?
                        for( var i = 0, j = el.length; i < j; i++ ) {
                            //call this function with each item in the array/collection
                            nodes = nodes.concat( arguments.callee(el[i]));
                        }
                        return nodes;
        }
            for( var i = 0, j = el.childNodes.length; i < j; i++ ) {
            var node = el.childNodes[i];
            if( node.nodeType == 3 ) {
                //ignore whitespace
                if( /^\s+$/.test( node.nodeValue ) ) continue;
                nodes.push( Tarjemlo.trim(node.nodeValue) );
            } else {
                //call this function with this child node
                nodes = nodes.concat( arguments.callee( node ) );
            }
            }
            return nodes;
    }

这篇关于jQuery不支持选择顶级nodetext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆