将模板文件动态加载到不适用于IE8的div中 - [英] Dynamic loading of template files into a div not working in IE8-

查看:111
本文介绍了将模板文件动态加载到不适用于IE8的div中 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码将包含我的模板的html文件动态加载到div中,它在所有浏览器中工作正常,除 IE8和更低



JS函数:

$ p $ 函数loadHTML(url,callback){
$ .ajax({
url:url,
成功:function(res){
var div = document.createElement(div);
div.setAttribute(id ,downloadIFrame);
document.body.appendChild(div);
document.getElementById(downloadIFrame)。innerHTML =(res);
callback();
}
});
}

template.html:

 < script type =text / htmlid =tmpl1> 
< div> sdfsdf< / div>
< / script>

< script type =text / htmlid =tmpl2>
< div> dddd< / div>
< / script>


解决方案

既然您已经在使用jQuery,为何不做如下所示:

 函数loadHTML(url,callback){
$ .get(url,function(res){
$('< div>')
.attr('id','downloadIFrame')
//在**
之后,将元素添加到DOM ** //设置html以提高性能
//操作已经存在于DOM中的元素
//计算量大
.html(res)
.appendTo('body')
; // div

if(callback)callback();
});
}


Im using the following code to load the html file containing my templates into a div dynamically it is working fine in all the browsers except IE8 and lower

JS Function:

function loadHTML(url, callback) {
    $.ajax({
        url: url,
        success: function(res) {
            var div = document.createElement("div");
            div.setAttribute("id", "downloadIFrame");
            document.body.appendChild(div);
            document.getElementById("downloadIFrame").innerHTML = (res);            
            callback();
        }
    });
}

template.html:

<script type="text/html" id="tmpl1">
    <div>sdfsdf</div>
</script>

<script type="text/html" id="tmpl2">
    <div>dddd</div>
</script>

解决方案

Since you're already using jQuery, why not do the following:

function loadHTML(url, callback) {
  $.get(url, function(res){
    $('<div>')
      .attr('id', 'downloadIFrame')
      // I add the element to the DOM **after**
      // setting the html to increase performance.
      // Manipulating elements already in the DOM
      // is computationally expensive
      .html(res)
      .appendTo('body')
    ;//div

    if(callback) callback();
  });
}

这篇关于将模板文件动态加载到不适用于IE8的div中 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆