Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - 将HTML插入div(Inserting HTML into a div)

I am trying to insert a chunk of HTML into a div.

(我试图将一大块HTML插入到div中。)

I want to see if plain JavaScript way is faster than using jQuery.

(我想看看简单的JavaScript方式是否比使用jQuery更快。)

Unfortunately, I forgot how to do it the 'old' way.

(不幸的是,我忘记了如何以“旧”的方式做到这一点。)

:P

(:P)

var test2 = function(){
    var cb = function(html){
        var t1 = document.getElementById("test2");
        var d = document.createElement("div");
        d.id ="oiio";
        d.innerHtml = html;
        t1.appendChild(d);
        console.timeEnd("load data with javascript");
    };
    console.time("load data with javascript");
    $.get("test1.html", cb);
}

what am i doing wrong here guys?

(我在这里做错了什么?)

edit :

(编辑 :)


Someone asked which is faster, jquery or plain js so i wrote up a test:

(有人问哪个更快,jquery或普通js所以我写了一个测试:)


http://jsperf.com/html-insertion-js-vs-jquery

(http://jsperf.com/html-insertion-js-vs-jquery)

plain js is 10% faster

(普通js快10%)

  ask by mkoryak translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is what you want:

(我想这就是你想要的:)

document.getElementById('tag-id').innerHTML = '<ol><li>html data</li></ol>';

Keep in mind that innerHTML is not accessable for all types of tags when using IE.

(请记住,使用IE时,innerHTML不能用于所有类型的标记。)

(table elements for example)

((例如表格元素))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...