【问题标题】:Loop through text nodes inside a div循环遍历 div 内的文本节点
【发布时间】:2011-02-01 07:03:05
【问题描述】:

我正在尝试进行文本替换,但要这样做,我需要遍历 div 的文本节点。

每个 Div 在单击时通过 ajax 加载它的适当内容。但是我需要在其中的任何文本节点内进行文本替换。

我现在的代码,在加载了ajax内容后,会循环遍历整个页面的所有文本节点,因此太耗费资源了。

我一直在寻找几个小时试图找出如何通过 div 循环并获取文本节点...

这必须在 firefox、google chrome 和 ie6 中工作。

有什么想法或建议吗?

根据要求,代码如下:

function ajaxLoader(url, id) {
    if (document.getElementById) {
        var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    }
    if (x) {
        x.onreadystatechange = function () {
            if (x.readyState == 4 && x.status == 200) {
                el = document.getElementById(id);
                el.innerHTML = x.responseText;
            }
        }
        x.open("GET", url, true);
        x.send(null);
    }
    // alert(id);
    CheckTranslate(id);
    // setTimeout('CheckTranslate(this);', 1000);

}

function CheckTranslate(id) {

    // function to get text of a node
    var content = function (node, txt) {
        if (txt) {
            if (node.textContent) {
                node.textContent = txt;
            } else if (node.nodeValue) {
                node.nodeValue = txt;
            }
        } else {
            return node.textContent ? node.textContent : node.nodeValue;
        }
    };
    // recuse div by id content
    $("#"+id).each(function() {
        // assign object handler
        var obj = $(this).html();

        // check how many text nodes there
        var mylen = obj.length;

        if (mylen > 0) {
            // loop thru each text node
        }
    });
    // recurse ajax content
    (function (parent) {
    var childs = parent.childNodes;

    // if there are children to this
    if (childs && childs.length) {

        // loop through each text node
        for (var i = 0, node; node = childs[i]; i++) {

        // text node found, do the replacement          
        if (node.nodeType == 3) { 

            // grab value of current text node
            var value = content(node);

            // grab class name of current text node
            var myclass = $(this).attr("class");

            // grab data property of this node
            var ist = $(this).data('translated');

            // check if this is correct class and has no data property and value is not undefined
            if (typeof(value) != 'undefined' && myclass != 'box_title' && ist != 'yes' && (myclass == 'status_bar' || myclass == '' || myclass == 'box_title_small' || myclass == 'status_bar_select')) {

                // loop thru english array to find matches
                for (var x = 0; x < en_count; x++) {

                    // get current english phrase
                    var from = en_lang[x];

                    // get current other language phrase
                    var to = other_lang[x];

                    if (value.match(from)) {

                        content(node, value.replace(from, to));
                        if ($.browser.msie == 'false') {
                            $(node).data('translated', 'yes');
                        }
                    }
                    // end check value match
                }
            }
        } else {
            arguments.callee(node);
        }
    }
    }
    })(document.body);
}   

有 2 个函数,ajaxLoader,它为 div 加载 ajax 内容,然后是 CheckTranslate,它在新内容加载后进行翻译。

问题是函数(父)部分查看所有文本节点,为了性能,我宁愿只查看 div 内的文本节点。

但我只是不明白如何引用那些......

这种东西很难弄清楚,jquery的文档也不是很容易学习...

【问题讨论】:

  • 发布您的代码,它会提示我们您对“文本节点”的含义。

标签: jquery html replace


【解决方案1】:

寻找这样的东西?

$(".your_ajax_div_class").each(function(i){});

您可能会在您的 ajaxComplete() 函数中附加或嵌套这个或类似的东西,以便在内容加载后触发。

【讨论】:

  • 但是如何识别和获取我的 div 中的文本节点?不,我不能使用类名,但在我的代码中,正在传递 div 的 id。所以我确实可以访问它。
  • 这将有助于查看您在查找/替换方面尝试执行的操作的示例。您是否试图访问 DIV 的特定子节点?如果没有,在我发布的函数中,只需访问 $(this).html() 即可访问 DIV 的内部 HTML。
【解决方案2】:

我确信有更好的方法可以做到这一点,但是在普通的 javascript 中,你可以只写一个递归函数:

function ReplaceChildText(node, findText, replaceText) {
    if (node.nodeType == 3) {
        node.innerHTML = node.innerHTML.replace(findText, replaceText);
    } else {
        for (var child in node.childNodes) {
            ReplaceChildText(child, findText, replaceText);
        }
    }
}

你可以这样称呼它:

ReplaceChildText(document.getElementById('divID'),'findText','replacement');

【讨论】:

  • 我的问题是如何只在 div 中这样做。没有通过整个页面。我有一段完全独立的代码可以完成那部分。
  • 如果只是在调用函数时传入div,它只会处理div及其子...
  • 我可以循环遍历 div 中的每个文本节点吗?
  • 是的,它就是这么做的。无论你传递什么,它都会查看它是否是一个文本节点。如果是这样,它会进行文本替换。如果不是,它会遍历它的每个子节点并对它们做同样的事情。
  • 很抱歉,但这对我不起作用。有没有办法改变 parent.childNodes 以引用 div id 的子节点?
【解决方案3】:

文本节点是指单词吗?

您可以使用split 将单词分隔成一个数组:

var str = $("div").text().split(' ');

这样的 div:

<div>red blue green orange</div>

会输出:

["red", "blue", "green", "orange"]

【讨论】:

  • 他的意思是DOM上下文中的文本节点。
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多