我创建了一个小网站,以下是我的文件:
https://github.com/S1BIOSE/E-business-Card
我把这些文件放在一个免费主机上,它使用HTML、JS和CSS。它不支持PHP。
我希望创建标记来替换JS、HTML、JSON、XML和TXT文件中的信息。例如:
发布于 2021-04-08 14:07:23
如果没有某种服务器端编程,就不能动态地更改文件。
在前端,您可以在URL中有标记,也可以通过阿贾克斯加载。
下面是一个在URL中使用查询字符串的示例:
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);<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>
https://stackoverflow.com/questions/67001808
复制相似问题