简体   繁体   English

如何用存储在本地存储中的文本替换 HTML 中的替换 {{content}} 占位符

[英]How to replace Replace {{content}} placeholder in HTML with text stored in local storage

I have a page named showdesign.html, it only has 4 lines of code which is:我有一个名为 showdesign.html 的页面,它只有 4 行代码,即:

 <script> var getData = JSON.parse(localStorage.getItem("templateType")); document.write(getData.template_code); console.log(getData.template_code); $("#main-wrapper").html("content of placeholder here"); </script>

When the page loads, it fetches the html structure from locally stored key templateType and object template_code .当页面加载时,它从本地存储的键templateType和对象template_code获取 html 结构。 The structure is shown below:结构如下图所示:

 <html> <head> </head> <body> <div id="main-wrapper"> {{content}} </div> </body> </html>

After the document.write is executed, I want to further replace the {{content}} with the key section231 and object description .执行 document.write 后,我想进一步将{{content}}替换为 key section231和 object description To do that I have written the code to add sample test in div main wrapper but this line does not work.为此,我编写了在 div 主包装器中添加示例测试的代码,但此行不起作用。

You don't need jQuery to do this, simply set the innerHTML with the value of your storage variable as follow:您不需要 jQuery 来执行此操作,只需使用存储变量的值设置innerHTML ,如下所示:

 // Store localStorage.setItem("content", "<span class='myclass'>my content</span>"); document.getElementById("main-wrapper").innerHTML = localStorage.getItem("content");
 .myclass { font-weight: bold; }
 <html> <head> </head> <body> <div id="main-wrapper"> {{content}} </div> </body> </html>

Please note : to run the above script on chrome you have to launch it from command line with this command:请注意:要在 chrome 上运行上述脚本,您必须使用以下命令从命令行启动它:

chrome.exe --disable-web-security

Try this code试试这个代码

var getData = JSON.parse('{' + localStorage.getItem("templateType") + '}');

Not sure but it might work for you不确定,但它可能对你有用

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

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