简体   繁体   English

Javascript替换标签中的文本,而不更改子元素的HTML和内容

[英]Javascript Replace Text in tags without changing children element HTML and Content

I have a script where I'd like to recolocar the text anywhere in the body visible to the user like so: 我有一个脚本,希望在其中将用户可见的正文中的文本重新表达出来,如下所示:

$("body").html($("body").html().replace(/replaced word/g,'HELLO'));

This works but it works to well. 这有效,但效果很好。 Whenever "replaced word" is within a tag, like a href, or span, or div, or anything it throws off all the formatting on the page. 每当“替换的单词”位于标签内时,例如href,span,div或其他任何东西,它都会抛出页面上所有的格式。 What is the best way to accomplish replacing the text for every occurrence in the page, no matter what element it's in, without effecting the page's functionality? 在不影响页面功能的情况下,无论页面中的每个元素是什么,完成替换页面中每次出现的文本的最佳方法是什么? Thanks 谢谢

EDIT: Thanks to talsibony and everyone's help I have an answer on how to replace words in child elements not effecting the html tags. 编辑:感谢塔尔西伯尼和每个人的帮助,我对如何替换不影响html标签的子元素中的单词有一个答案。 I'd like to have the replacement with a font color or other styling options like so: 我想用字体颜色或其他样式选项代替,例如:

$(element).html($(element).selectorText().replace(/thisi/g,'<abbr title="hover title"><font color="#10a9cf">HELLO</font></abbr>'));

The weird thing is this code works in jsfiddle but when I include this in the google chrome extension I'm working on and the page loads the page is empty. 奇怪的是,此代码在jsfiddle中有效,但是当我将其包含在google chrome扩展程序中时,我正在工作,并且页面加载时页面为空。 When I switch it back to selectortext it loads but I see the html tags in the replacement. 当我将其切换回选择器文本时,它会加载,但在替换中会看到html标签。

I can't understand why because a staright replace doesn't work and doesn't let the page load either! 我不明白为什么,因为繁琐的替换操作不起作用,也不让页面加载!

$(element).html($(element).selectorText().replace(/thisi/g,'HELLO'));

In the console I get the following error: Cannot read property 'replace' of null 在控制台中,我收到以下错误: 无法读取null的属性“ replace”

What am I missing here? 我在这里想念什么? Thanks again! 再次感谢!

The way I think you will have to do it is in each element individually and use this jquery small plugin I rewrite here is the code and also 我认为您将必须执行的方式是在每个元素中单独使用,并使用此jquery小插件重写代码,

fiddle 小提琴

the html HTML

<div id="parent">
    thisi sthe fpcd
    <p>p</p>
</div>

plugin to find the content of the selector text without child elements 插件来查找没有子元素的选择器文本的内容

$.fn.selectorText = function(text) {
    var str = '';

    this.contents().each(function() {
        if (this.nodeType === 3) {
            if(text){
                this.textContent = text;
                return false;
            }else{
                str += this.textContent || this.innerText || '';
            }

        }
    });

    return str;
};

using it for your case 用于您的情况

allowReplaceTags = ['DIV'];
$('body').find('*').each(function (i, element) {
    if ($.inArray(allowReplaceTags,element.tagName)){
        // do replace
        $(element).selectorText($(element).selectorText().replace(/thisi/g,'HELO'));

    }

});

$(“ body”)。html($(“ body”)。text()。replace(/ replaced word / g,'HELLO'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM