Jquery 查找并替换HTML字符串。忽略URL

Jquery 查找并替换HTML字符串。忽略URL,jquery,dom,replace,Jquery,Dom,Replace,我已经编写了一个基本脚本来搜索DOM中的字符串并替换。但在当前状态下,它也会影响链接的URL 例如,将“示例”替换为“我的示例”将破坏以下示例 <a href="http://my example.com/my example">My Example</a> 从性能角度来看,大约有80个单词需要查找/替换,是否有更好的处理方法?当然,每种语言都缺少单独的页面 如果使用.text()而不是.html(),则不会更改HREF $(document).ready(functi

我已经编写了一个基本脚本来搜索DOM中的字符串并替换。但在当前状态下,它也会影响链接的URL

例如,将“示例”替换为“我的示例”将破坏以下示例

<a href="http://my example.com/my example">My Example</a>
从性能角度来看,大约有80个单词需要查找/替换,是否有更好的处理方法?当然,每种语言都缺少单独的页面

如果使用.text()而不是.html(),则不会更改HREF

$(document).ready(function(){
    $("body").children().each(function() {           
        $(this).text($(this).text().replace(/example/g,"my example"));
    });
});
更新:我应该补充一点,这可能会导致目前所写的意外副作用。我建议将.replace操作限制为原子元素,如下所示:

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {
        var children = object.children();
        if (children.length > 0) {
            children.each(function() {
                replaceText($(this));
            });
        } else {
            object.text(object.text().replace(/example/g, "my example"));
        };
    };
});
更新:这里有一种适用于自由浮动文本节点的替代方法

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {

        //first we handle text nodes inside the element itself
        handleTextNodes(object); 

        //then we iterate down into the child elements 
        var children = object.children();
        children.each(function() {
            replaceText($(this));
        });
    };

    function handleTextNodes(object) {
        object.contents().filter(function() {
            return this.nodeType == 3;
        }).each(function() {
            textNodeReplace(this);
        });
    };

    function textNodeReplace(node) {
        var text = node.nodeValue;
        node.nodeValue = text.replace(/example/g, "my example");
    };
});
如果使用.text()而不是.html(),则不会更改HREF

$(document).ready(function(){
    $("body").children().each(function() {           
        $(this).text($(this).text().replace(/example/g,"my example"));
    });
});
更新:我应该补充一点,这可能会导致目前所写的意外副作用。我建议将.replace操作限制为原子元素,如下所示:

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {
        var children = object.children();
        if (children.length > 0) {
            children.each(function() {
                replaceText($(this));
            });
        } else {
            object.text(object.text().replace(/example/g, "my example"));
        };
    };
});
更新:这里有一种适用于自由浮动文本节点的替代方法

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {

        //first we handle text nodes inside the element itself
        handleTextNodes(object); 

        //then we iterate down into the child elements 
        var children = object.children();
        children.each(function() {
            replaceText($(this));
        });
    };

    function handleTextNodes(object) {
        object.contents().filter(function() {
            return this.nodeType == 3;
        }).each(function() {
            textNodeReplace(this);
        });
    };

    function textNodeReplace(node) {
        var text = node.nodeValue;
        node.nodeValue = text.replace(/example/g, "my example");
    };
});

谢谢你的快速回复。使用此代码,我得到一个错误。。。未捕获的TypeError:无法读取未定义Doops的属性“createDocumentFragment”-我忘了更新最后一行。现在应该可以了,太好了!真是一种享受!非常感谢你!实际上,刚刚注意到这个方法只替换每个子对象中的第一个实例。有没有办法让它以特定子对象中的所有实例为目标?它应该适用于所有实例,但不适用于文本节点(即在div中自由浮动的文本)。所以这可能就是问题所在。如果是这样的话,我已经添加了另一种方法。感谢您的快速回复。使用此代码,我得到一个错误。。。未捕获的TypeError:无法读取未定义Doops的属性“createDocumentFragment”-我忘了更新最后一行。现在应该可以了,太好了!真是一种享受!非常感谢你!实际上,刚刚注意到这个方法只替换每个子对象中的第一个实例。有没有办法让它以特定子对象中的所有实例为目标?它应该适用于所有实例,但不适用于文本节点(即在div中自由浮动的文本)。所以这可能就是问题所在。如果是这样,我添加了一种替代方法。