Javascript 我有一个按钮可以在表单中显示表格,但它可以在表单外部显示表格。如何在表单中显示 功能板尺寸(id,btnid) { 功能HITHEARD(id,btnid){ 返回函数(){ console.log(id); }; } 函数createTable(行、列、元素){ 函数getButtonId(){ 返回“命中” } var table=document.createElement('table'), tr,td,button,i,j, 颜色=[“红色”、“蓝色”、“黄色”]; 对于(i=0;i

Javascript 我有一个按钮可以在表单中显示表格,但它可以在表单外部显示表格。如何在表单中显示 功能板尺寸(id,btnid) { 功能HITHEARD(id,btnid){ 返回函数(){ console.log(id); }; } 函数createTable(行、列、元素){ 函数getButtonId(){ 返回“命中” } var table=document.createElement('table'), tr,td,button,i,j, 颜色=[“红色”、“蓝色”、“黄色”]; 对于(i=0;i,javascript,html,forms,Javascript,Html,Forms,function boardSize(id, btnid) { function hitheadr(id,btnid) { return function () { console.log(id); }; } function createTable(rows, cols, element) { function getButtonI

function boardSize(id, btnid)
    {
        function hitheadr(id,btnid) {

            return function () {
                console.log(id);

          };

        }

        function createTable(rows, cols, element) {
            function getButtonId( ) {

                return 'hit'
           }

            var table = document.createElement('table'),
                tr, td, button, i, j,
                colors = ["red", "blue", "yellow"];

            for (i = 0; i < rows; i++) {
                tr = document.createElement('tr');

                for (j = 0; j < cols; j++) {
                    td = document.createElement('td');
                    button = document.createElement('button');
                    button.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    button.appendChild(document.createTextNode(getButtonId(i, j)));
                    button.onclick = hitheadr(getButtonId(i, j));
                    button.id = getButtonId(i, j);
                    button.onclick = onBtnClick;

                    // button.innerHTML = 'HIT';
                    /* document.getElementById(getButtonId(i, j)).onclick = function () {
                     alert('should ');

                     };*/


                    button.id = getButtonId(i, j);
                    td.appendChild(button);
                    tr.appendChild(td);
                }
                table.appendChild(tr);
                table.setAttribute("align", "center");
                tr.setAttribute("border", "2");
            }
            element.appendChild(table);
        }

        createTable(3, 3, document.body);
        document.getElementById(btnid).disabled = true;
        document.getElementById(btnid).style.backgroundColor = "#00CCFF";
        document.getElementById('le').disabled = true;
        document.getElementById('se').disabled = true;
    }

function boardSize(id, btnid)
    {
        function hitheadr(id,btnid) {

            return function () {
                console.log(id);

          };

        }

        function createTable(rows, cols, element) {
            function getButtonId( ) {

                return 'hit'
           }

            var table = document.createElement('table'),
                tr, td, button, i, j,
                colors = ["red", "blue", "yellow"];

            for (i = 0; i < rows; i++) {
                tr = document.createElement('tr');

                for (j = 0; j < cols; j++) {
                    td = document.createElement('td');
                    button = document.createElement('button');
                    button.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    button.appendChild(document.createTextNode(getButtonId(i, j)));
                    button.onclick = hitheadr(getButtonId(i, j));
                    button.id = getButtonId(i, j);
                    button.onclick = onBtnClick;

                    // button.innerHTML = 'HIT';
                    /* document.getElementById(getButtonId(i, j)).onclick = function () {
                     alert('should ');

                     };*/


                    button.id = getButtonId(i, j);
                    td.appendChild(button);
                    tr.appendChild(td);
                }
                table.appendChild(tr);
                table.setAttribute("align", "center");
                tr.setAttribute("border", "2");
            }
            element.appendChild(table);
        }

        createTable(3, 3, document.body);
        document.getElementById(btnid).disabled = true;
        document.getElementById(btnid).style.backgroundColor = "#00CCFF";
        document.getElementById('le').disabled = true;
        document.getElementById('se').disabled = true;
    }

function boardSize(id, btnid)
    {
        function hitheadr(id,btnid) {

            return function () {
                console.log(id);

          };

        }

        function createTable(rows, cols, element) {
            function getButtonId( ) {

                return 'hit'
           }

            var table = document.createElement('table'),
                tr, td, button, i, j,
                colors = ["red", "blue", "yellow"];

            for (i = 0; i < rows; i++) {
                tr = document.createElement('tr');

                for (j = 0; j < cols; j++) {
                    td = document.createElement('td');
                    button = document.createElement('button');
                    button.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    button.appendChild(document.createTextNode(getButtonId(i, j)));
                    button.onclick = hitheadr(getButtonId(i, j));
                    button.id = getButtonId(i, j);
                    button.onclick = onBtnClick;

                    // button.innerHTML = 'HIT';
                    /* document.getElementById(getButtonId(i, j)).onclick = function () {
                     alert('should ');

                     };*/


                    button.id = getButtonId(i, j);
                    td.appendChild(button);
                    tr.appendChild(td);
                }
                table.appendChild(tr);
                table.setAttribute("align", "center");
                tr.setAttribute("border", "2");
            }
            element.appendChild(table);
        }

        createTable(3, 3, document.body);
        document.getElementById(btnid).disabled = true;
        document.getElementById(btnid).style.backgroundColor = "#00CCFF";
        document.getElementById('le').disabled = true;
        document.getElementById('se').disabled = true;
    }
我有一个按钮在表单中显示表格,但它在表单外部显示表格我如何在表单中显示
如何在表单help me中显示按钮的内容。

在代码中,您正在将表作为主体的子对象创建,这就是为什么它出现在表单的最后一个外部。在createTable函数中,将第三个参数作为希望表出现的表单传递。作为
createTable(3,3,document.querySelector('form'))

我看不到表单。但我看到一些无效的HTML。
function boardSize(id, btnid)
    {
        function hitheadr(id,btnid) {

            return function () {
                console.log(id);

          };

        }

        function createTable(rows, cols, element) {
            function getButtonId( ) {

                return 'hit'
           }

            var table = document.createElement('table'),
                tr, td, button, i, j,
                colors = ["red", "blue", "yellow"];

            for (i = 0; i < rows; i++) {
                tr = document.createElement('tr');

                for (j = 0; j < cols; j++) {
                    td = document.createElement('td');
                    button = document.createElement('button');
                    button.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    button.appendChild(document.createTextNode(getButtonId(i, j)));
                    button.onclick = hitheadr(getButtonId(i, j));
                    button.id = getButtonId(i, j);
                    button.onclick = onBtnClick;

                    // button.innerHTML = 'HIT';
                    /* document.getElementById(getButtonId(i, j)).onclick = function () {
                     alert('should ');

                     };*/


                    button.id = getButtonId(i, j);
                    td.appendChild(button);
                    tr.appendChild(td);
                }
                table.appendChild(tr);
                table.setAttribute("align", "center");
                tr.setAttribute("border", "2");
            }
            element.appendChild(table);
        }

        createTable(3, 3, document.body);
        document.getElementById(btnid).disabled = true;
        document.getElementById(btnid).style.backgroundColor = "#00CCFF";
        document.getElementById('le').disabled = true;
        document.getElementById('se').disabled = true;
    }