日期:2014-05-16  浏览次数:20428 次

做一个乘法表,就像九九乘法表那样的,可是不要九,而是输入几,就生成一个几几的乘法表。急,高手请指教!
做了半天也没有个头绪,请大家帮帮忙。
才学了一周javascrpit,老师就让做这个

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> JavaScript 9*9 </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<style>
td {
    font-size: 12px;
}
</style>

<body leftmargin="0" topmargin="0">
    <br />
    <br />
    <input type="text" id="id_txt" maxlength="2" style="width: 80px" onblur="doCal(this)"> input a number
    <br />
    <br />
    <table id="id_tbl" border="1" cellpadding="0" cellspacing="0">
    </table>
</body>
</html>

<script language="JavaScript">
<!--
    function doCal(obj) {
        var num = obj.value;
        var tbl = document.getElementById("id_tbl");

        if (document.body.all)
        {
            tbl.innerText = "";
        }
        else
        {
            tbl.innerHTML = "";
        }

        if (isNaN(num) || num.trim() == "")
        {
            return;
        }

        for (var i = 1; i <= num; i++)
        {
            var newRow = tbl.insertRow(tbl.rows.length);
            for (var j = 1; j <= i; j++)
            {
                var newCell = document.createElement("td");
                newCell.innerHTML = i + "*" + j + " = " + i * j;
                newRow.appendChild(newCell);
            }

            for (j; j <= num; j++)
            {
                var newCell = document.createElement("td");
                newCell.innerHTML = "&nbsp;";
                newRow.appendChild(newCell);
            }
        }
    }

    String.prototype.trim = function() {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    };
//-->
</script>