1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

document.implementation & Range

Posted at

document.implementation と document.createRange、
range.selectNodeContentsなどを組み合わせて
テキストからDOMを抽出する方法。

var xhr = new XMLHttpRequest();
xhr.open('GET', 'test.html');
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        hoge(xhr.responseText);
    }
};
xhr.send(null);

function hoge(txt) {
    var doc = document.implementation.createHTMLDocument('');
    var range = doc.createRange();

    range.selectNodeContents(doc.documentElement);
    range.deleteContents();
    doc.documentElement.appendChild(range.createContextualFragment(txt));

    range.selectNodeContents(doc.documentElement);
    var cont = range.extractContents();
    console.log(cont);
    document.body.appendChild(cont);
}
}());
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?