browser as editor
html

browser as editor

data:text/html,\<html contenteditable/>以上代码并不 work,在 JavaScript 中,使用window.open方法打开的新标签页不能直接编辑。data:...

花野猫

花野猫

更新于 2023-09-21

800

data:text/html,<html contenteditable/>

js
window.open('data:text/html,<html contenteditable/>')

以上代码并不 work,在 JavaScript 中,使用window.open方法打开的新标签页不能直接编辑。data:text/html URL 方案只能用于展示静态的 HTML 内容。

要在新标签页中打开一个可编辑的标签页,可以尝试以下方法:

方法1:使用document.opendocument.write

javascript
var newWindow = window.open();
newWindow.document.open();
newWindow.document.write('<html contenteditable><body></body></html>');
newWindow.document.close();

方法2:使用createElementappendChild

javascript
var newWindow = window.open();
var editableDiv = newWindow.document.createElement('div');
editableDiv.contentEditable = true;
newWindow.document.body.appendChild(editableDiv);