首页 > 解决方案 > 如何使用 JavaScript 在新窗口中从主页打开功能?

问题描述

我试图弄清楚如何让我的 HTML 按钮在按下时打开 JavaScript 函数,在新选项卡中打开这些函数,这样我就可以返回并单击新选项卡。这是大学作业,教授不想超过一页,必须打开功能(加法和乘法表分开)。

我一直在尝试从使变量成为全局变量到将变量嵌套到 main() 变量中的所有方法,但没有任何效果。它指出未定义加法和乘法函数。

我对 JavaScript 很陌生,因为这是我的第一个学期,如果这是非常基础的,我深表歉意。

<!DOCTYPE html>
<head>
<title>Math Tables</title>
<script type="text/javascript">
function multiplicationTable(table){
    document.write("<table border='1' width='300' cellspacing='0' cellpadding='3'>");
    document.write("<tr>");
    document.write("<td> * </td>");
    for (x = 1; x <=10; x++){
    document.write("<td>"+ x +"</td>");
    }
for (y = 1; y <= 10; y++) {
    document.write( "<tr>");
    document.write( "<td>" + y + "</td>");
for (z = 1; z <= 10; z++) {
    document.write("<td>" + (y * z) + "</td>");
    }
    document.write( "</tr>");
    }
    document.write( "</table>");
}

function additionTable(){
    document.write("<table border='1' width='300' cellspacing='0' cellpadding='3'>");
    document.write("<tr>");
    document.write("<td> * </td>");
for (x = 1; x <=10; x++){
    document.write("<td>"+ x +"</td>");
}

for (y = 1; y <= 10; y++) {
    document.write( "<tr>");
    document.write( "<td>" + y + "</td>");
for (z = 1; z <= 10; z++) {
    document.write("<td>" + (y + z) + "</td>");

        }
    }   
}

</script>
</head>
<body>
<h1>Math Tables</h1>

<p>
<input type="button" value="Addition Table" id="addition" onclick="additionTable();"/><br />
<input type="button" value="Multiplication Table" id="multiplication"onclick="multiplicationTable();" /></p>

</body>
</html>

标签: javascripthtmlfunctionfor-loop

解决方案


推荐阅读