嘿,我有一个 HTML 块,我将重复使用(在用户访问期间的不同时间,而不是一次)。我认为实现这一点的最佳方法是创建一个 HTML div,将其隐藏,并在需要时获取其 innerHTML 并对几个关键字执行 replace()。作为示例 HTML 块...
<div id='sample'>
<h4>%TITLE%</h4>
<p>Text text %KEYWORD% text</p>
<p>%CONTENT%</p>
<img src="images/%ID%/1.jpg" />
</div>
用动态数据替换这些关键字的最佳方法是去...
template = document.getElementById('sample');
template = template.replace(/%TITLE%/, some_var_with_title);
template = template.replace(/%KEYWORD%/, some_var_with_keyword);
template = template.replace(/%CONTENT%/, some_var_with_content);
template = template.replace(/%ID%/, some_var_with_id);
感觉就像我选择了一种愚蠢的方式来做到这一点。有没有人对如何以任何方式更快、更智能或更好地做到这一点有任何建议?此代码将在用户访问期间相当频繁地执行,有时每 3-4 秒执行一次。
提前致谢。