如何将HTML字符串转换为HTML文档?

Flo*_*ent 9 html javascript parsing html-parsing

我想在HTML文档中转换下面的HTML字符串.下面的代码适用于Firefox和Chrome,但不适用于其他浏览器(Opera,Safari,IE).你能帮助我吗?

var content = '<iframe width="640" height="360" src="//www.youtube.com/embed/ZnuwB35GYMY" frameborder="0" allowfullscreen></iframe>';

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(content,"text/html");
Run Code Online (Sandbox Code Playgroud)

谢谢你不要在JQuery中回复.

我想做到这一点,但在javascript中

<?php
    $content = '<iframe width="640" height="360" src="//www.youtube.com/embed/ZnuwB35GYMY" frameborder="0" allowfullscreen></iframe>';
    $doc = new DOMDocument();
    $doc->loadHTML($content);
?>
Run Code Online (Sandbox Code Playgroud)

我的主要目标是在HTML文档中转换HTML文本,以便更改iframe的Width和Height属性.

Del*_*yer 0

使用 HTML5:

var impl    = document.implementation,
    htmlDoc = impl.createHTMLDocument(title);
Run Code Online (Sandbox Code Playgroud)

http://www.w3.org/TR/html5/dom.html#dom-domhtmlimplementation-createhtmldocument

创建后,您可以使用 document.write() 对其进行编辑。

注意:此功能仅适用于最新的浏览器