如何让IFrame一次重绘所有内容

时间:2018-11-30 19:59:45

标签: javascript html iframe

我正在尝试制作降价编辑器,但是我的IFrame出现问题。它将IFrame源设置为about:blank来清除屏幕,但是直到按下键之前它不会再次写入iframe。我该如何解决?

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Markdown Edit</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <div class="editor">
        <textarea id="textbox" cols="30" rows="10" oninput="updatePreview()"></textarea>
    </div>

    <iframe frameborder="0" id="preview"></iframe>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/8.4.2/markdown-it.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="scripts.js"></script>
</body>
</html>

JS:

window.onerror = function(msg, url, linenumber) {
    alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
}

var textbox = document.getElementById("textbox");
var preview = document.getElementById("preview");
var md = window.markdownit();

preview.contentWindow.document.open();

function updatePreview() {
    preview.src = "about:blank";
    preview.contentWindow.document.write(md.render(textbox.value));
}

1 个答案:

答案 0 :(得分:0)

更新

您可以在按键功能上使用jquery吗?那么,在cuser完成输入之后,它将更新吗?

$( "#target" ).keyup(function() {
  #update iframe code here
});

您可以每30秒刷新一次iframe吗?

<script>
window.setInterval("reloadIFrame();", 30000);

function reloadIFrame() {
 document.frames["framename"].location.reload();
}
</script>

我总是保存代码的和平。这很棒。