gpt4 book ai didi

javascript - Ace Editor 中 iframe 源代码输出中出现不需要的空间

转载 作者:行者123 更新时间:2023-12-03 11:54:02 26 4
gpt4 key购买 nike

在我的项目中,我使用 Ace Editor 和 iframe。 Ace 编辑器包含来自 iframe 的源代码。

加载网页时,源代码会加载到 Ace 编辑器中。我使用此代码将源代码从 iframe 加载到 Ace 编辑器:

function getCode()
{
var code = "<!DOCTYPE html>" + "\n" + document.getElementById("iframe_id").contentWindow.document.documentElement.outerHTML;
editor.getSession().setValue(code);
}

我必须手动输出“<!DOCTYPE html>”,因为

document.getElementById("iframe_id").contentWindow.document.documentElement.outerHTML 

不输出文档类型。

当网页加载时,Ace Editor 会加载 iframe 源代码:

<body onLoad="getCode()">

但是 Ace Editor 中 iframe 的源代码如下所示:

<!DOCTYPE html>
<html><head>
<title>Learning HTML</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is your first webpage.</p>



</body></html>

但我希望代码能够像这样正确排列:

<!DOCTYPE html>
<html>
<head>
<title>Learning HTML</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is your first webpage.</p>
</body>
</html>

不知道问题出在哪里。请帮忙。

更新: - 当我右键单击并查看 iframe 的源代码时,代码与 Ace 编辑器中显示的相同。

最佳答案

如果您想实现此目的,则必须先将代码通过 HTML 美化器(如 js-beautify ),然后再在 Ace 中显示。 Ace 本身没有这个选项。

另请参阅this question .

关于javascript - Ace Editor 中 iframe 源代码输出中出现不需要的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25682563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com