0
我有一個example.html
文件:分配HTML片段到JS變量
<span>
{{test}}
</span>
而且一個main.js
文件:
$("button#test").click(function(event) {
var html = '/example.html';
//Replaceing {{test}} in example.html with 'Hello, World!' string
var insertProperty = function (string, propName, propValue) {
var propToReplace = "{{" + propName + "}}";
string = string
.replace(new RegExp(propToReplace, "g"), propValue);
return string;
}
var replacedhtml = insertProperty(html,"test", 'Hello, World!');
return console.log(replacedhtml);
});
我目前得到的日誌:
/example.html
我預計:
<span>
Hello, World!
</span>
而且應該有一個比我的insertProperty
函數更優雅的插入屬性的方法。
寫作'變種HTML =「/ example.html''創建一個字符串,而不是檢索從文件中的HTML文本。後者需要使用'$ .ajax'。 – gyre
你爲什麼不用模板引擎如ejs,玉 –