下面有一个简单的html表。现在它直接显示出来了。相反,我们可以在这里添加一个按钮,这样当用户点击按钮时,下表应该会打开
<table border=1>
<tr>
<th> Ratings </th>
<th> Text </th>
</tr>
<tr>
<td> 1</td>
<td > asd </td>
</tr>
<tr>
<td> 2 </td>
<td > dfdsf </td>
</tr>
</table>
您可以打开一个新窗口,然后将表注入其中,如下所示:
const table = `<table border=1>
<tr>
<th> Ratings </th>
<th> Text </th>
</tr>
<tr>
<td> 1</td>
<td > asd </td>
</tr>
<tr>
<td> 2 </td>
<td > dfdsf </td>
</tr>
</table>`;
const popup = window.open();
popup.document.body.innerHTML = table;
注意:该代码段在StackOverflow上不起作用,因为iframe
策略不允许弹出窗口。
这是一种简单的方法:
注意:Stackoverflow没有正确运行代码段,请将其下载到本地,然后运行。
<html>
<script>
function openWindow() {
var newtab = window.open("", "anotherWindow", "width=300,height=150");
newtab.document.write("<table border=1><tr><th>Ratings</th><th>Text</th></tr><tr><td>1</td><td>asd</td></tr><tr><td>2</td><td>dfdsf</td></tr></table>");
}
</script>
<body>
<b> Click the button to open the new user-defined sized window </b>
<br><br>
<button onclick="openWindow()"> Open Window </button>
</body>
</html>
您可以使用(https://developer.mozilla.org/en-US/docs/Web/API/Window/open(
function myFunction() {
var myWindow = window.open("", "MsgWindow", "width=200, height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and
100px
tall!</p>");
}