2015-03-25 40 views
0

在运行时添加淘汰赛模板清空HTML DOM在运行时添加淘汰赛模板清空HTML DOM

var templateEngine = new ko.nativeTemplateEngine(); 
    templateEngine.addTemplate = function (templateName, templateMarkup) { 
     document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "<" + "/script>"); 
    }; 
    templateEngine.addTemplate("gridTable","<table></table"); 

所有较早的内容,去和DOM成为

<html><head><script type="text/html" id="gridView"><table></table></script></head></html> 
+2

https://developer.mozilla.org/en-US/docs/Web/API/Document/write:在关闭的(加载的)文档上调用'document.write' **会自动调用'document.open',它将清除文件** – nemesv 2015-03-25 07:44:20

回答

0

我会建议你创建在您的html结构中分隔div,然后在该div中加载模板。例如,你可以使用jQuery来做它。

$("#someDiv").append(templateMarkup); 
1

正如@nemsev说,

eveloper.mozilla.org/en-US/docs/Web/API/Document/write:在一个封闭的(加载)文件,要求文件撰写自动调用document.open这将清除文档

所以,我修改了我的代码,

var templateEngine = new ko.nativeTemplateEngine(); 

    templateEngine.addTemplate = function (templateName, templateMarkup) { 
     //document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "<" + "/script>"); 
     var scriptTag = document.createElement("script"); 
     scriptTag.type = "text/html"; 
     scriptTag.id = templateName; 
     scriptTag.innerHTML = templateMarkup; 
     var node = document.getElementsByTagName("head")[0]; 
     node.appendChild(scriptTag); 
    }; 

    templateEngine.addTemplate("gridTable","<table></table");