Javascript 替换所有单词

发布于 2024-10-12 15:26:24 字数 734 浏览 6 评论 0原文

我需要使用 jquery 和 javascript 替换 DOM(内存中的网页)中的所有单词。 我需要将其转换

<html>
<head>
    Hi to all
</head>
<body>
    Hi to all
</body>
</html>

为:

<html>
    <head>
        Bye to all
    </head>
    <body>
        Bye to all
    </body>
</html>

但不修改标签(仅其中的值)

一种尝试是:

jQuery.fn.replaceEachOne = function (objective, rep) {
    return this.each( function(){
                $(this).html( $(this).html().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}

但它会继续替换标签值并在我放置它时破坏页面。

帮助!!!

I need to replace all the words in a DOM (web page in memory) using jquery and javascript.
I need to transform this:

<html>
<head>
    Hi to all
</head>
<body>
    Hi to all
</body>
</html>

Into this:

<html>
    <head>
        Bye to all
    </head>
    <body>
        Bye to all
    </body>
</html>

But without modifying the tags (ONLY THE VALUES IN THERE)

One try is:

jQuery.fn.replaceEachOne = function (objective, rep) {
    return this.each( function(){
                $(this).html( $(this).html().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}

But it continue replacing the tag values and breaks the page if I put it.

Help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆流 2024-10-19 15:26:24

我猜测标题中会有其他元素,而不只是用新文本替换所有内容。尝试这样的事情

$(function(){
  var element = $('body, head').find('*:contains("Hi to all")');
  element.html('Bye to all');
});

Im guessing that there are going to be other elements in the header rather then just replacing everything with the new text. Try out something like this

$(function(){
  var element = $('body, head').find('*:contains("Hi to all")');
  element.html('Bye to all');
});
花期渐远 2024-10-19 15:26:24

如果您不希望它更改您的标签,为什么要修改“html”而不仅仅是文本?

jQuery.fn.replaceEachOne = function (objective, rep) {
    return this.each( function(){
                $(this).text( $(this).text().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}

if you don't want it to change your tags, why do you modify the "html"and not just the text?

jQuery.fn.replaceEachOne = function (objective, rep) {
    return this.each( function(){
                $(this).text( $(this).text().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}
触ぅ动初心 2024-10-19 15:26:24

一种虽然效率低下的方法是:

var element = document.getElementsByTagName('html')[0];
element.innerHTML = element.innerHTML.replace('Hi to all', 'Bye to all');

如果需要,引号中的“Hi to all”也可以替换为正则表达式。

One way that would do it albeit inefficiently would be:

var element = document.getElementsByTagName('html')[0];
element.innerHTML = element.innerHTML.replace('Hi to all', 'Bye to all');

The 'Hi to all' in quotes can also be replaced with a regex if you want.

滴情不沾 2024-10-19 15:26:24

终于……

谢谢大家了……

jQuery.fn.replaceEachOne = function (objective, reposition) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(objective, reposition);
        } else if (this.nodeType == Node.TEXT_NODE) {
            var label = document.createElement("label");
            label.innerHTML = this.nodeValue.replace(objective, reposition);
            this.parentNode.insertBefore(label, this);
            this.parentNode.removeChild(this);
        }
    }); 
}

Finally...

Thanks to everyone...

jQuery.fn.replaceEachOne = function (objective, reposition) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(objective, reposition);
        } else if (this.nodeType == Node.TEXT_NODE) {
            var label = document.createElement("label");
            label.innerHTML = this.nodeValue.replace(objective, reposition);
            this.parentNode.insertBefore(label, this);
            this.parentNode.removeChild(this);
        }
    }); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文