在JavaScript中,如何替换HTML页面中的文本而不影响标签? [英] In JavaScript, how can I replace text in an HTML page without affecting the tags?

查看:200
本文介绍了在JavaScript中,如何替换HTML页面中的文本而不影响标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何用Javascript替换。我正在查看页面的整个主体,并希望在HTML标签中替换关键字匹配NOT。



以下是一个示例:

 < body> 
< span id =keyword> blah< / span>
< div>
blah blah关键字blah< br />
无论什么关键字
< / div>
< / body>

< script type =text / javascript>
var replace_terms = {
'keyword':{'url':'http://en.wikipedia.org/','target':'_blank'}
}

jQuery.each(replace_terms,function(i,val){
var re = new RegExp(i,gi);
$('body')。html $ b $('body')。html()。replace(re,'< a href ='+ val ['url'] +'target ='+ val ['target'] +'> ;'+ i +'< / a>')
);
});

< / script>

我正在寻找替换不在HTML标签内的所有关键字的实例(< > 之间)



我想我还需要忽略keyword是否在脚本 style 元素中。

解决方案

不要使用正则表达式来解析HTML。 [X] [HT] ML不是常规语言,无法使用正则表达式进行可靠的处理。您的浏览器内置了一个好的HTML解析器;这样就可以在标签所在的地方找到工作。



另外你也不希望在 html()/ innerHTML 在身上。这将会对整个页面进行序列化和重新解析,这将很慢,并且会丢失HTML中无法序列化的任何信息,例如事件处理程序,表单值和其他JavaScript引用。



这是一个使用DOM的方法,似乎适用于我:

  function replaceInElement(element,find,replace){ 
//反向遍历子节点,因为替换可能会增加
//子节点列表的长度。
for(var i = element.childNodes.length; i - > 0;){
var child = element.childNodes [i];
if(child.nodeType == 1){// ELEMENT_NODE
var tag = child.nodeName.toLowerCase();
if(tag!='style'&& tag!='script')//特殊情况下,不要碰CDATA元素
replaceInElement(child,find,replace);
} else if(child.nodeType == 3){// TEXT_NODE
replaceInText(child,find,replace);
}
}
}
function replaceInText(text,find,replace){
var match;
var matches = [];
while(match = find.exec(text.data))
matches.push(match);
for(var i = matches.length; i - > 0;){
match = matches [i];
text.splitText(match.index);
text.nextSibling.splitText(match [0] .length);
text.parentNode.replaceChild(replace(match),text.nextSibling);
}
}

//要匹配的关键字。这个*必须是一个'g'全局正则表达式,否则会失败
var find = / \b(keyword | whatever)\b / gi;

//用wiki链接替换匹配的字符串
replaceInElement(document.body,find,function(match){
var link = document.createElement('a');
link.href ='http://en.wikipedia.org/wiki/'+match[0];
link.appendChild(document.createTextNode(match [0]));
return link;
});


I'm trying to figure out how to do a replace with Javascript. I'm looking at the entire body of the page and would like to replace the keyword matches NOT within an HTML tag.

Here is an example:

<body>
  <span id="keyword">blah</span>
  <div>
    blah blah keyword blah<br />
    whatever keyword whatever
  </div>
</body>

<script type="text/javascript">
var replace_terms = {
  'keyword':{'url':'http://en.wikipedia.org/','target':'_blank'}
}

jQuery.each(replace_terms, function(i, val) {
  var re = new RegExp(i, "gi");
  $('body').html(
    $('body').html().replace(re, '<a href="'+ val['url'] +'" target="'+val['target']+'">' + i + '</a>')
  );
});

</script>

I'm looking to replace all instances of the "keyword" that isn't within an HTML tag (between < and >).

I guess I also need to ignore if "keyword" is within a script or style element.

解决方案

Don't use regex to parse HTML. [X][HT]ML is not a regular language and cannot reliably be processed using regex. Your browser has a good HTML parser built-in; let that take the strain of working out where the tags are.

Also you don't really want to work on html()/innerHTML on body. This will serialise and re-parse the entire page, which will be slow and will lose any information that cannot be serialised in HTML, such as event handlers, form values and other JavaScript references.

Here's a method using DOM that seems to work for me:

function replaceInElement(element, find, replace) {
    // iterate over child nodes in reverse, as replacement may increase
    // length of child node list.
    for (var i= element.childNodes.length; i-->0;) {
        var child= element.childNodes[i];
        if (child.nodeType==1) { // ELEMENT_NODE
            var tag= child.nodeName.toLowerCase();
            if (tag!='style' && tag!='script') // special case, don't touch CDATA elements
                replaceInElement(child, find, replace);
        } else if (child.nodeType==3) { // TEXT_NODE
            replaceInText(child, find, replace);
        }
    }
}
function replaceInText(text, find, replace) {
    var match;
    var matches= [];
    while (match= find.exec(text.data))
        matches.push(match);
    for (var i= matches.length; i-->0;) {
        match= matches[i];
        text.splitText(match.index);
        text.nextSibling.splitText(match[0].length);
        text.parentNode.replaceChild(replace(match), text.nextSibling);
    }
}

// keywords to match. This *must* be a 'g'lobal regexp or it'll fail bad
var find= /\b(keyword|whatever)\b/gi;

// replace matched strings with wiki links
replaceInElement(document.body, find, function(match) {
    var link= document.createElement('a');
    link.href= 'http://en.wikipedia.org/wiki/'+match[0];
    link.appendChild(document.createTextNode(match[0]));
    return link;
});

这篇关于在JavaScript中,如何替换HTML页面中的文本而不影响标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆