你的思路沒問題,監聽輸入事件,然后將iframe拼接代碼即可,代碼如下:
<!DOCTYPE?html>
<html>
?<head>?
??<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>?
??<title>練習</title>?
??<style?type="text/css">
#codeArea{
width:500px;
height:500px;
float:left;
border:1px?solid?red;
}
#resultArea{
width:100%;
height:500px;
border:1px?solid?green;
}
#result{
margin-left:520px;
}
</style>?
?</head>?
?<body>?
??<div?id="code">?
???<textarea?id="codeArea">登錄</textarea>?
??</div>?
??<div?id="result">?
???<iframe?id="resultArea">?</iframe>?
??</div>??
??<script?type="text/javascript">?
var?codeArea?=?document.getElementById("codeArea");
var?resultArea?=?document.getElementById("resultArea");
codeArea.oninput?=?function(){
resultArea.contentWindow.document.body.innerHTML?=?codeArea.value;
}
??</script>?
?</body>
</html>