在JS函数中打开一个新的HTML页面,然后在其上写一些HTML

时间:2016-12-31 18:53:04

标签: javascript html

我有这个 main.html 文件,其中有一个“按钮”类型的按钮。它有一个 create()函数来处理 onclick 事件。

我还有另一个页面 test.html ,它与 main.html 位于同一个文件夹中。

我想要的是什么:

  1. 单击按钮后, test.html 即会打开
  2. create()在其上写一些html,比如“Hello World”
  3. 我不明白该怎么做。我读到了window.open(),但我想这只是一个弹出窗口,我想要一个完整的页面。

2 个答案:

答案 0 :(得分:3)

在main.html中:



<input type='button' onclick='location.href=test.html' value='click me'>
&#13;
&#13;
&#13;

test.html中的

&#13;
&#13;
function create() {
  // Write something in the page
}
&#13;
<body onload='create()'>
  
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

function create()
{
    window.location = 'test.html';
}

这是你的出发点。然后,您需要在test.html文件中添加一些PHP或JS。

方法1:PHP

例如,您可以从网址获取您要撰写的邮件。

主档案:     function create()     {         window.location =&#39; test.php?p = Hello%World&#39 ;;     } 测试文件:               

方法2:JS

您还可以在测试文件中编写一个JS函数,该函数会在加载页面后立即调用。

主档案:

&#13;
&#13;
function create() // Create function on 1st page
{
  window.location = 'test.html';
}
&#13;
&#13;
&#13;

测试文件:

&#13;
&#13;
<head>
  <script type="text/javascript">
    function message()
    {
      document.getElementById('container').innerHTML = 'Hello world';
    }
  </script>
</head>

<body onload="message();">
  <div id="container"></div>
</body>
&#13;
&#13;
&#13;

希望它有所帮助。