替换每个HTML控件的内容javascript jquery

替换每个HTML控件的内容javascript jquery,javascript,jquery,html,replace,Javascript,Jquery,Html,Replace,我再次尝试使用javascript和jquery替换页面中每个控件的每个文本内容 我需要在每个文本内容中搜索任何单词(没有修改标签,只有文本),并用另一个单词替换它 一个尝试是: jQuery.fn.replaceEachOne = function (objective, rep) { this.each( function(){ //$(this).html( $(this).html().replace( new RegExp('(\\s'+obje

我再次尝试使用javascript和jquery替换页面中每个控件的每个文本内容

我需要在每个文本内容中搜索任何单词(没有修改标签,只有文本),并用另一个单词替换它

一个尝试是:

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

请帮忙

像这样的事情应该可以做到:

$.fn.replaceEachOne = function(search, replace) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(search, replace);
        } else if (this.nodeType == Node.TEXT_NODE) {
            this.nodeValue = this.nodeValue.replace(search, replace);
        }
    });
};

这直接在文本节点上进行替换,而不是修改整个元素的HTML。请注意,它区分大小写。如果需要不区分大小写的搜索,则需要将调用更改为
replace
,以使用正则表达式。

类似的操作应该可以:

$.fn.replaceEachOne = function(search, replace) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(search, replace);
        } else if (this.nodeType == Node.TEXT_NODE) {
            this.nodeValue = this.nodeValue.replace(search, replace);
        }
    });
};

这直接在文本节点上进行替换,而不是修改整个元素的HTML。请注意,它区分大小写。如果需要不区分大小写的搜索,则需要将调用更改为
replace
以使用正则表达式。

我将从以下内容开始:

jQuery.fn.replaceEachOne = function(objective, rep) {
    var html = jQuery(this).html();
    var simpleRegexp = new RegExp(objective, "gi");
    var regexp = new RegExp(">[^><]*?"+objective+"[^><]*?<","gi");
    html = html.replace(regexp, function(match) {
        return match.replace(simpleRegexp, rep);
    });
    jQuery(this).html(html);
}
jQuery.fn.replaceAchone=函数(目标,代表){
var html=jQuery(this.html();
var simpleRegexp=新的RegExp(目标“gi”);

var regexp=new regexp(“>[^>我将从以下内容开始:

jQuery.fn.replaceEachOne = function(objective, rep) {
    var html = jQuery(this).html();
    var simpleRegexp = new RegExp(objective, "gi");
    var regexp = new RegExp(">[^><]*?"+objective+"[^><]*?<","gi");
    html = html.replace(regexp, function(match) {
        return match.replace(simpleRegexp, rep);
    });
    jQuery(this).html(html);
}
jQuery.fn.replaceAchone=函数(目标,代表){
var html=jQuery(this.html();
var simpleRegexp=新的RegExp(目标“gi”);
var regexp=new regexp(“>[^>最后

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);
        }
    }); 
}
最后

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);
        }
    }); 
}

这是可行的,但我必须为搜索文本添加一个新控件,我的意思是添加:搜索文本,如果我这样做,浏览器不会将其识别为控件,只识别为文本,我的意思是,它显示的是文本搜索文本,而不是颜色不同的酷标签…:(帮助!!@Daniel G.R.听起来你需要。这很有效,但我必须在搜索文本中添加一个新控件,我的意思是添加:搜索文本,如果我这样做,浏览器不会将其识别为控件,只识别为文本,我的意思是,它是一个文本搜索文本,而不是一个颜色不同的酷标签…:(帮助!!@Daniel G.R.听起来你需要。但问题是我不想在我需要在这里添加内容的地方添加内容我写了“beetween'>”和“编辑信息:我用这个替换了“body”。我试过:jQuery(“body”).replaceAchone(“jQuery”,“jQuery”);在jQuery.org上,它显示了该页面上所有jQuery事件的颜色。但问题是我不想在页面中添加内容,我需要在这里添加内容。我写了“beetween”>“编辑信息:我用这个替换了“body”。我试过:jQuery(“body”).replaceAchone(“jQuery”,“jQuery”);并显示该页面上所有jQuery事件的颜色。