![](/img/trans.png)
[英]How to get a reference to an iframe's window object inside iframe's onload handler created from parent window
[英]How to get reference to Codemirror inside an iframe from parent page?
我有一个parent.html
页面,我在一个 div 元素中嵌入了一个Iframe
,而 iframe 的src
引用了一个显示代码镜像的codemirror.html
页面。
<div id="cmModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<iframe id="cmIframeId" name="cmiframe" src="../html/codemirror.html"></iframe>
</div></div>
当我单击一个button
时,我显示一个包含iframe
作为模式块的 div。 这时我想通过调用 Code-mirror 实例来初始化Code-mirror
。
如何从 parent.html 初始化 CodeMirror 内容?
我做了什么但仍然无法初始化 codemirror:
在codemirror.html
页面上声明了 codemirror 的全局实例和全局 function。
var codeMirrorEditor; window.setCode = function(content) { codeMirrorEditor.setValue(content);; }
从 parent.html 调用那些全局即时和parent.html
function openCodeMirror(){ // Get the modal var modal = document.getElementById("cmModal"); modal.style.display = "block"; var iframe = document.getElementById("cmIframeId"); iframe.contentWindow.setCode('My initial value'); iframe.contentWindow.codeMirrorEditor.setValue('My initial value') }
codemirror.html
<!doctype html>
<html lang="en">
<head>
<title>CodeMirror: HTML EDITOR</title>
// all codmirror addons and script comes here ...
</head>
<body>
<div id="code" ></div>
<script>
var codeMirrorEditor;
window.setCode = function(content) {
codeMirrorEditor.setValue(content);;
}
window.onload = function () {
codeMirrorEditor = CodeMirror(document.getElementById("code"), {
mode: "text/html",
styleActiveLine: true,
lineNumbers: true,
lineWrapping: true,
autoCloseTags: true,
theme: "night",
value: getOwnSource(),
});
};
function setContent(content) {
codeMirrorEditor.setValue(content);
}
function getContent() {
return codeMirrorEditor.getValue();
}
function getOwnSource() {
return document.documentElement.innerHTML;
}
</script>
</body>
</html>
父母.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="../css/modal.css">
</head>
<body>
<textarea id="source" style="width:100%; height:400px"> initial text comes from here </textarea><br>
<input type="button" value="open codemirror" onclick="openCodeMirror()"> </input>
<!-- The Codemirror Modal window-->
<div id="cmModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<iframe id="cmIframeId" name="cmiframe" src="../html/codemirror.html" style="width:100%; height:300px"></iframe>
</div>
</div>
<script>
function openCodeMirror(){
var modal = document.getElementById("cmModal");
modal.style.display = "block";
var iframe = document.getElementById("cmIframeId");
let textarea = document.getElementById("source");
iframe.contentWindow.codeMirrorEditor.setValue(textarea.value);
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
</script>
</body>
</html>
modal.css 对于 parent.html
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 0px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
/* background-color: #ff8000; */
background-color:#fefefe ;
margin-left: 0px;
margin-right: 0px;
margin-top: 0px;
margin-bottom: 0px;
padding-left: 0px;
padding-right: 0px;
padding-top: 0px;
padding-bottom: 0px;
/* padding: 10px; */
border: 1px solid #888;
width: 100%;
height:100%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 32px;
font-weight: bold;
margin-right: 10px;
/* border: 1px solid #ff0000; */
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
伙计们,它就像魅力一样,这只是我混合代码的错误。 清理代码后,它可以正常工作。 感谢大家,它可以帮助别人解决其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.