Javascript 替换非';不在任何标签中
我使用这段代码来获取body中所有节点的文本(浏览器是IE8,它在FF上工作…) 谢谢,我开发了一个工具栏来更改页面中的一些文本,所以如果你有任何其他解决方案,我会很高兴,如果我用正则表达式替换所有正文,它会禁用一些网站的未来。例如,在facebook中,如果使用替换所有建议,未来将在朋友查找部分禁用,所以我想一步一步地更改单词,这是一个过滤坏话的过滤器Javascript 替换非';不在任何标签中,javascript,replace,html,Javascript,Replace,Html,我使用这段代码来获取body中所有节点的文本(浏览器是IE8,它在FF上工作…) 谢谢,我开发了一个工具栏来更改页面中的一些文本,所以如果你有任何其他解决方案,我会很高兴,如果我用正则表达式替换所有正文,它会禁用一些网站的未来。例如,在facebook中,如果使用替换所有建议,未来将在朋友查找部分禁用,所以我想一步一步地更改单词,这是一个过滤坏话的过滤器 function replaceText(node) { var textPropName = node.textContent === u
function replaceText(node)
{
var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';
var childs = node.childNodes, i = 0;
while(i < childs.length)
{
alert(childs[i][textPropName]);
if (childs[i][textPropName] !=null)
{
childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
}
else
{
replaceText(document.body.childNodes[i]);} i++;
}
}
}
最终解决方案
function replaceText(node){
var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';
var childs = node.childNodes, i = 0;
var len= node.childNodes.length;
// alert('len='+len);
//var rgx = new RegExp('\\bold\\b', 'gi');
while(node = childs[i]){
//alert('ee'+node.nodeType);
if (node.nodeType == 3 ) {
//alert('!!!!!'+node.nodeValue );
//alert('ee'+node.nodeType);
childs[i].nodeValue = childs[i].nodeValue.replace(rgx, 'newText');
} else {
replaceText(node);
}
给你,这也适用于IE
函数替换文本(节点)
{
var textPropName=node.textContent==未定义?'innerText':'textContent';
var childs=node.childNodes,i=0;
var rgx=new RegExp('\\bff\\b | \\byyy\\b','gi');
而(i
使用content()属性:-它获取匹配元素集中每个元素的子元素,包括文本和注释节点
试试这个:
// in jquery
$("body").contents().filter(function() {
return this.nodeType == 3;
}).replaceWith("hello");
// in javascript
var nodes = document.getElementsByName('body').childNodes
for (i=0; i < nodes.length; i++)
{
if(nodes.childNode[i].nodeType == 3)
{
//THIS NODE IS A TEXT NODE OF THE BODY ELEMENT
//DO WHAT YOU NEED WITH IT
}
}
//在jquery中
$(“body”).contents().filter(函数()){
返回this.nodeType==3;
}).替换为(“你好”);
//在javascript中
var nodes=document.getElementsByName('body').childNodes
对于(i=0;i
参见此处如何调用函数
replaceText
?我从h1标记onClick=“replaceText(document.body)调用它rgx的值是多少?
?这并不重要,就像var rgx=new RegExp('\\bff\\b|\\byyy\\b','gi一样;但我用'alert(childs[i][textPropName]检查child的值)'所有不在标记中的文本都不会显示在messagebox中,只有在标记中的文本才会被警告您使用的浏览器是哪一个?@joseph-所以您想用勺子喂他…??让他从这个示例中了解一下。这个示例对OP没有任何帮助。他们没有使用jQuery,也无法访问其API。谢谢,但我更改了您的代码to这一点但不起作用,联机var nodes=..get error对象不支持此属性或方法它是文档。getElementsByName()
问题:请访问此url更改sex word页面后为什么会更改并仅显示文本?
function replaceText(node)
{
var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';
var childs = node.childNodes, i = 0;
var rgx = new RegExp('\\bff\\b|\\byyy\\b', 'gi');
while(i < childs.length)
{
if(childs[i].nodeType == 3) {
alert(childs[i].nodeValue);
childs[i].nodeValue = childs[i].nodeValue.replace(rgx,'new');
} else {
alert(childs[i][textPropName]);
if (childs[i][textPropName] !=null)
{
childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
}
else
{
replaceText(document.body.childNodes[i]);
}
}
i++;
}
}
// in jquery
$("body").contents().filter(function() {
return this.nodeType == 3;
}).replaceWith("hello");
// in javascript
var nodes = document.getElementsByName('body').childNodes
for (i=0; i < nodes.length; i++)
{
if(nodes.childNode[i].nodeType == 3)
{
//THIS NODE IS A TEXT NODE OF THE BODY ELEMENT
//DO WHAT YOU NEED WITH IT
}
}