简体   繁体   English

如何使用javascript替换正文中的文本

[英]how to replace text in the body using javascript

I have the following code to replace all instances of "onAppDeactivate" with "onAppActivate" in the entire body of the document, but it doesn't seem to be working at all. 我有以下代码在文档的整个正文中将“ onAppDeactivate”的所有实例替换为“ onAppActivate”,但似乎根本无法正常工作。 Any ideas? 有任何想法吗?

var pageSource = document.body.getElementsByTagName("script");

var replaceWithThis = "onAppActivate";
for(var i=0, l=pageSource.length; i<l; i++)
{
    pageSource[i].innerHTML.replace(/onAppDeactivate/gi, replaceWithThis)
}

You can't replace text in a script file or script block that has already been parsed. 您不能替换已经解析的脚本文件或脚本块中的文本。 It won't do anything. 它什么也不会做。

You can replace entire functions in your javascript by actually assigning new code to the function name in javascript (not with text manipulation). 您可以通过向javascript中的函数名称实际分配新代码来替换javascript中的整个函数(不使用文本操作)。

For example, if there was a function already defined: 例如,如果已经定义了一个函数:

function foo() {
   // do something
}

You, could replace that with your own function: 您可以将其替换为您自己的函数:

function foo() {
   // do something different
}

In your pastebin code, you can disable the onBlur() and onFocus() global functions by running this code after the original code in the page runs. 在您的pastebin代码中,可以在页面中的原始代码运行之后运行此代码来禁用onBlur()和onFocus()全局函数。

window.onBlur = window.onFocus = function() {};

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

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