首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用标记替换文件中的信息而不使用PHP?

问如何用标记替换文件中的信息而不使用PHP?
EN

Stack Overflow用户
提问于 2021-04-08 10:10:33
回答 1查看 93关注 0票数 0

我创建了一个小网站,以下是我的文件:

https://github.com/S1BIOSE/E-business-Card

我把这些文件放在一个免费主机上,它使用HTML、JS和CSS。它不支持PHP。

我希望创建标记来替换JS、HTML、JSON、XML和TXT文件中的信息。例如:

  • 有公司名称的记号。
  • 带有网站网址的令牌。
  • 与公司的描述相符的记号。
EN

回答 1

Stack Overflow用户

发布于 2021-04-08 14:07:23

如果没有某种服务器端编程,就不能动态地更改文件。

在前端,您可以在URL中有标记,也可以通过阿贾克斯加载。

下面是一个在URL中使用查询字符串的示例:

​

代码语言:javascript
复制
function LoadAndApplyTokens() {
  const urlParams = new URLSearchParams(window.location.search);
  document.querySelectorAll('*').forEach(tag => {
    for (let i = 0; i < tag.childNodes.length; i++) {
      const child = tag.childNodes[i];
      if (child.nodeName.toLowerCase() != '#text') {
        continue;
      }
      let val = child.nodeValue;
      let change = false;
      let variables = val.match(/::[^:]+::/img);
      if (variables == null)
        continue;
      variables.forEach(variable => {
        if (urlParams.has(variable.substr(2, variable.length - 4))) {
          const variableValue = urlParams.get(variable.substr(2, variable.length - 4));
          while (val.indexOf(variable) >= 0) {
            val = val.replace(variable, variableValue);
          }
          change = true;
        }
      });
      if (change) {
        child.nodeValue = val;
        console.log(child.parentNode, val);
      }
    }
  });
}
window.addEventListener("load", LoadAndApplyTokens);
代码语言:javascript
复制
<div>
  <h1>My name is <b>::name::</b></h1>
  <p><b>Description of ::name::</b><br/>::name:: is a company</p>
</div>
<form method="get">
  <label>Name: <input name="name" /></label>
  <button type="submit">Change</button>
</form>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67001808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档