[英]text/template inside js-file?
我想将此代码从我的html文件移动到js文件,是否有任何流畅的方法可以做到这一点而不必将 HTML 代码作为字符串?
<script type="text/template" id="template_test">
<h4>Test</h4>
<p>blablabla</p>
</script>
谢谢!
您可以使用 createElement 函数来创建 HTML 元素,但是如果您打算移动大量代码,您应该考虑使用模板引擎。
// Create the <h4> element
var heading = document.createElement("h4");
// add the text
heading.textContent = "TEST";
// Create the text node <p>
var p = document.createTextNode("blablabla");
//get the element to which you want to append the h4 and p
var container = document.getElementById("myID");
// Append h4 and p
container.appendChild(heading);
container.appendChild(p);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.