以可在浏览器中执行的方式用 Javascript 替换 HTML

发布于 2024-12-07 23:07:30 字数 216 浏览 0 评论 0原文

我根本没有 JavaScript 经验。我想要的是替换页面 HTML 中文本块的单个实例 - 我该如何做到这一点?

30 分钟的阅读让我意识到:

javascript:document.body.innerHTML = document.body.innerHTML.replace("this","that");

我已经接近了吗?

I have no Javascript experience at all. What I want is to replace a single instance of a block of text in a page's HTML - how can I do this?

30 minutes of reading around has brought me this:

javascript:document.body.innerHTML = document.body.innerHTML.replace("this","that");

Am I even close?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-12-14 23:07:30

由于没有任何经验,我建议您看一下 jQuery。使用 jQuery,您可以执行以下操作:

给定:

<p>block of text</p>

jQuery:

$('p').text("some other block of text");

With no experiance at all I recommend you take a look at jQuery. With jQuery you can do:

Given:

<p>block of text</p>

jQuery:

$('p').text("some other block of text");
紫轩蝶泪 2024-12-14 23:07:30
javascript:document.body.innerHTML = "that"
javascript:document.body.innerHTML = "that"
才能让你更想念 2024-12-14 23:07:30

1) 如果它是 URL 的一部分,例如 ,那么您需要
javascript:void(document.body.innerHTML = document.body.innerHTML.replace("this","that"));

2) 如果它是事件的一部分,例如

3) 如果您尝试将“this”的所有实例替换为“that”,并且不仅仅是第一个,那么你需要
....replace(/this/g,"that")

1) If it is part of a URL, such as <a href="...">, then you need
javascript:void(document.body.innerHTML = document.body.innerHTML.replace("this","that"));

2) If it is part of an event, such as <button onClick="...">, then you need
document.body.innerHTML = document.body.innerHTML.replace("this","that");

3) If you are trying to replace ALL instances of "this" with "that", and not just the first, then you need
... .replace(/this/g,"that")

九八野马 2024-12-14 23:07:30

您不能只在地址栏中执行该脚本。它需要对一个文档进行操作,但是那里没有任何东西可以替代。从地址栏执行 javascript 将为您提供一个新的空文档,该代码将在该文档上运行。

即使您尝试从 JavaScript 加载文档,脚本的其余部分也会首先执行。试试这个:

javascript:window.location='http://www.google.com';alert(document.innerHTML);

您会看到在页面加载之前弹出警报,并且显示“未定义”。

即使您尝试绑定到文档或窗口的 onload 事件,它也不会起作用。可能是因为它们之后被重置。

javascript:window.location='http://www.google.com';window.onload=function(){alert(document.innerHTML);};

这是有道理的;如果这可行,您可以在跳转到下一个页面时操作该页面,从而可以在链接到的页面中注入 javascript。这将是一个很大的安全问题,所以这是一件好事,这不起作用。

You cannot just execute that script in the address bar. It needs to operate on a document, but there is nothing to replace there. Executing javascript from the address bar will give you a new empty document on which that code operates.

Even if you try to load a document from javascript, the rest of your script gets executed first. Try this:

javascript:window.location='http://www.google.com';alert(document.innerHTML);

You'll see that the alert pops up before the page is loaded, and it shows 'undefined'.

Even when you try binding to the onload event of the document or the window it won't work. Probably because they are reset afterwards.

javascript:window.location='http://www.google.com';window.onload=function(){alert(document.innerHTML);};

And it makes sense; if this would work, you could manipulate the next page when jumping to that page, thus making it possible to inject javascript in a page you link to. That would be a big security issue, so it's a good thing this doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文