0

如何在同一窗口的框架上写新行?现在我让它用window.open创建一个新窗口。但现在我需要它在一个框架中。

我有一个包含两个框架(表单和结果)的 html,我需要通过按下第一框架上的按钮来写入第二个框架。

HTML

<html>
    <head>
    <title> Create Curriculum</title>
    <meta charset="UTF-8"/>
    </head>
    <frameset rows="50%,50%">
        <frame id="form" src="from.html"/>
        <frame id="curri" src=""/>
    </frameset>

您与表单交互,将信息添加到输入并保存信息。还有一个按钮可以使用下面输入的信息生成课程表。

JAVASCRIPT:

function genCurriculum() {
    //I want to capture the second frame with id "curri"
    var curri = parent.document.getElementById("curri");
    curri.document.writeln("<h2>Head of the curriculum...</h2>");
    //The others writeln for creating the curriculum
}

我想知道如何在框架中而不是在新窗口中书写。

并且不使用Jquery,只是纯粹JavaScript的。(教师要求)

4

1 回答 1

0

你想要发生的事情吗?

html:

<form>
    <input name="nextLine" type="text" />
    <button type="button">
        <span>Add a line</span>
    </button>
</form>
<iframe height="100" width="200"></iframe>

JavaScript:

(function () {
    'use strict';
    var button, iframe, input;
    function addLine (cD, input, val) {
        return function () {
            var body, line;
            body = cD.getElementsByTagName('body')[0]
            line = cD.createElement('p');
            line.innerText = val.toString() + ': ' + input.value;
            body.appendChild(line);
            val += 1;
        }
    }
    button = document.getElementsByTagName('button')[0];
    iframe = document.getElementsByTagName('iframe')[0];
    input = document.getElementsByTagName('input')[0];
    button.addEventListener('click', addLine(iframe.contentDocument, input, 0), true);
}());
于 2013-11-14T17:24:10.797 回答