gpt4 book ai didi

javascript - 如何以正确的方式在 Javascript 中插入 HTML 文本?

转载 作者:行者123 更新时间:2023-11-28 15:32:37 25 4
gpt4 key购买 nike

我有这个代码:

 var p = document.createElement("p"); 

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you&#39;ll find blogger templates, a freelancers area, Question &amp;&nbsp;Answers Forum, create your own blog and will be able to interact with other people.<br />
Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.";
document.body.appendChild(p);

但是控制台返回错误。我检查了一下,看起来 html 代码破坏了 javascript 代码。该错误直接指向这一行“欢迎来到 Akecheta,网络战士社交网络。在这里您将找到博客模板,”

如果我无法澄清我的问题,我很抱歉,我不知道如何用语言表达。发生的情况是,由于该 html 内容,整个脚本没有运行,我相信它的格式错误。

编辑 1

完整代码:

<script>// <![CDATA[
var regex = new RegExp("/testurl/$");
if(document.URL.match(regex))
{

var p = document.createElement("p");

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \& Answers Forum. You can even create your own blog to interact with other people.<br/><br/>Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.";
document.body.appendChild(p);

}
else {
document.write('');
}
// ]]></script>

编辑2

所以我尝试了这个:

<script>// <![CDATA[
var regex = new RegExp("/free-blogger-templates/$");
if(document.URL.match(regex))
{
document.write('Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \& Answers Forum. You can even create your own blog to interact with other people.<br/>Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.');
}
else {
document.write('');
}
// ]]></script>

而且它完全有效。但是这段代码是在所见即所得编辑器上插入的,因此每当我将 html 编辑器更改为可视化编辑器并返回到 html 编辑器时,文本代码就会被删除。

最终编辑

这是我的代码,完全正常工作,该代码应该根据浏览器地址隐藏/显示文本,如果您想使用它,只需将 /free-blogger-templates/ 更改为您将在其中运行脚本的当前页面,例如,如果您在 yoursitedotcom/myfirstpage 上运行,请将其更改为 /myfirstpage/$,$ 表示它将只允许该网址,直到正斜杠 /,我不得不使用 document.write,我知道不推荐它,但这是我找到的唯一解决方案,这里是:

<script>// <![CDATA[
var regex = new RegExp("/free-blogger-templates/$"); if(document.URL.match(regex)) { document.write('Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \&amp; Answers Forum. You can even create your own blog to interact with other people. Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.'); } else { document.write(''); }
// ]]></script>

文本必须正确转换(感谢用户@God is good(是的,上帝真的很好=]),然后我使用http://www.web2generators.com/html/entities对HTML文本进行编码。

最佳答案

JavaScript 中的字符串不能有换行符。这是一种替代方案。

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you&#39;ll find blogger templates, a freelancers area, Question &amp;&nbsp;Answers Forum, create your own blog and will be able to interact with other people.<br />" +
"Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.";
document.body.appendChild(p);

关于javascript - 如何以正确的方式在 Javascript 中插入 HTML 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com