繁体   English   中英

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

[英]Open a new HTML page in a JS function and then write some HTML on it

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

我还有另一个页面test.htmlmain.html放在同一文件夹中。

我想要的是:

  1. 单击按钮后, test.html就会打开
  2. create()在上面写一些html,例如“ Hello World”

我不知道该怎么做。 我读到有关window.open()的信息,但我想这只是一个弹出窗口,我想要一个完整的页面。

在main.html中:

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

在test.html中

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

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

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

方法1:PHP

例如,您可以从URL中获取要编写的消息。

主文件:function create(){window.location ='test.php?p = Hello%World'; 测试文件:

方法2:JS

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

主文件:

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

测试文件:

 <head> <script type="text/javascript"> function message() { document.getElementById('container').innerHTML = 'Hello world'; } </script> </head> <body onload="message();"> <div id="container"></div> </body> 

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM