我在另一个div中有ace div,我希望ace编辑器将它的宽度和高度调整为父div。我调用editor.resize()但没有任何反应。
<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
#editor {
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100px;
}
</style>
</head>
<body style="height: 100%">
<div style="background-color: red; height: 100%; width: 100%;">
<div id="editor">function foo(items) {
var x = "All this is syntax highlighted";
return x;
}</div>
</div>
<script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
editor.resize();
</script>
</body>
</html>
你可以用两种方式达到你想要的效果。我创造了一个 的jsfiddle 显示用于将ace编辑器的大小调整为其容器的css和javascript。
使用的css是为了使编辑器占用容器的宽度和高度,这样做 editor.resize()
可以正确计算编辑器的大小。
我推荐以下内容来获取 editor.resize()
上班。
<style type="text/css" media="screen">
#editor {
width: 100%;
height: 100%;
}
</style>
但是,如果您想维护使用当前的css #editor
以下将有效。
<style type="text/css" media="screen">
#editor {
position: absolute; /* Added */
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
并添加 position: relative;
到容器,以便绝对定位的编辑器正确定位在其容器内。至于这是如何工作的,我推荐你 绝对定位内部相对定位。
你可以用两种方式达到你想要的效果。我创造了一个 的jsfiddle 显示用于将ace编辑器的大小调整为其容器的css和javascript。
使用的css是为了使编辑器占用容器的宽度和高度,这样做 editor.resize()
可以正确计算编辑器的大小。
我推荐以下内容来获取 editor.resize()
上班。
<style type="text/css" media="screen">
#editor {
width: 100%;
height: 100%;
}
</style>
但是,如果您想维护使用当前的css #editor
以下将有效。
<style type="text/css" media="screen">
#editor {
position: absolute; /* Added */
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
并添加 position: relative;
到容器,以便绝对定位的编辑器正确定位在其容器内。至于这是如何工作的,我推荐你 绝对定位内部相对定位。
运用 jQuery的王牌 我通过使用简单地管理了这个:
$('#php_code').ace({
theme: 'chrome',
lang: 'php',
width: '100%',
height: '300px'
})
我使用简单的CSS:
#container{
height:80vh;
}
#editor {
width: 100%;
height: 100%;
position: relative;
}
关键的css属性是 position:relative
,凌驾于违约之上 position:absolute
ace编辑器,它生成父容器无法调整其内容。
<div id="container">
<pre id="editor">
<div>
		this is a div
	</div>
</pre>
</div>
<script>
$(document).ready(function() {
var editor = ace.edit("editor");
editor.setTheme("ace/theme/TextMate");
editor.session.setMode("ace/mode/html");
});
</script>
将其设置重置为浏览器默认设置,根据设计适合父容器。
#editor {
width: inherit !important;
}
我正在使用react-ace包装器来反应。
对于任何覆盖了一些默认值的ace包装器可能是有益的。