0

次のコードを使用して、テンプレートを含む html ファイルを動的に div に読み込みます。IE8 and lower

JS 関数:

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>
4

1 に答える 1

0

すでに jQuery を使用しているので、次のことを行ってみませんか。

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();
  });
}
于 2013-01-25T07:31:00.090 に答える