Javascript 替换所有单词
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜测标题中会有其他元素,而不只是用新文本替换所有内容。尝试这样的事情
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
如果您不希望它更改您的标签,为什么要修改“html”而不仅仅是文本?
if you don't want it to change your tags, why do you modify the "html"and not just the text?
一种虽然效率低下的方法是:
如果需要,引号中的“Hi to all”也可以替换为正则表达式。
One way that would do it albeit inefficiently would be:
The 'Hi to all' in quotes can also be replaced with a regex if you want.
终于……
谢谢大家了……
Finally...
Thanks to everyone...