在表td中给出div类名

时间:2014-05-01 22:02:32

标签: javascript class html appendchild

我试图在另一个div中给一堆div同一个类名,并且通过这样做,我希望将每个子div放入表中的td。

我无法更改html / css表。

希望你能帮忙!

http://jsfiddle.net/6mkYL/1/

HTML:

<div id="puzzlearea">
            <!-- the following are the fifteen puzzle pieces -->
            <div>1</div>  <div>2</div>  <div>3</div>  <div>4</div>
            <div>5</div>  <div>6</div>  <div>7</div>  <div>8</div>
            <div>9</div>  <div>10</div> <div>11</div> <div>12</div>
            <div>13</div> <div>14</div> <div>15</div>
        </div>

javascript:

function loading () {
    function nameBox () {
        var bigBox = document.getElementById("puzzlearea");
        var divs = bigBox.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            divs[i].className = "boxes";
        }
    }
    function tableCreate () {
        var body = document.getElementById('puzzlearea');
        var tbl = document.createElement('table');
        tbl.style.width = '400px';
        tbl.style.height = '400px';
        tbl.cellPadding = '0';
        tbl.cellSpacing = '0';
        tbl.setAttribute('border', '2');
        var tbdy = document.createElement('tbody');
        for (var i = 0; i < 4; i++) {
            var tr = document.createElement('tr');
            for (var j = 0; j < 4; j++) {
                if (i == 3 && j == 4) {break} else {
                    var td = document.createElement('td');
                    td.appendChild("boxes");
                    tr.appendChild(td)
                }
            }
            tbdy.appendChild(tr);
        }
        tbl.appendChild(tbdy);
        body.appendChild(tbl)
    }
    tableCreate();
};
window.onload = function () {
    loading();
};

3 个答案:

答案 0 :(得分:1)

这应该这样做,你有一堆语法错误和逻辑中的一些失误,但我保持程序的整体格式相同:

function loading () {
    function nameBox () {
        var bigBox = document.getElementById("puzzlearea");
        var divs = bigBox.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            divs[i].className = "boxes";
        }

        // Notice I return the node list of divs so you can use it to populate
        // your grid in tableCreate
        return divs;
    }
    function tableCreate () {
        var body = document.getElementById('puzzlearea'),
            tbl  = document.createElement('table'),
            boxes = nameBox();

        // I would recommend removing this block and simply setting these
        // style attributes in CSS under an id that you give to your table
        tbl.style.width = '400px';
        tbl.style.height = '400px';
        tbl.cellPadding = '0';
        tbl.cellSpacing = '0';
        tbl.setAttribute('border', '2');

        var tbdy = document.createElement('tbody');
        for (var i = 0; i < 4; i++) {
            var tr = document.createElement('tr');

            // Notice how I've added boxes.length as a loop condition
            // this will automatically break the loop once the boxes
            // node list is empty
            for (var j = 0; j < 4 && boxes.length; j++) {
                    var td = document.createElement('td');

                    // You were appending the string "boxes" here
                    // I've changed it to append the first div
                    // remaining in the boxes node list
                    td.appendChild([].shift.call(boxes););
                    tr.appendChild(td)
            }
            tbdy.appendChild(tr);
        }
        tbl.appendChild(tbdy);
        body.appendChild(tbl)
    }
    tableCreate();
};
window.onload = function () {
    loading();
};

答案 1 :(得分:0)

我不确定这一点,但也许你应该尝试把你的拼图区域和div作为全局变量.. .. ..

function loading()
{
var bigBox = document.getElementById('puzzlearea'),
`divs = document.getElementsByTagName('div'),i;

function nameBox()
{
for( i=0; i<divs.length; i++ )
{
divs[i].className = 'boxes';
}
}
}

然后继续使用另一个函数来修改变量..

答案 2 :(得分:0)

我认为你试图实现这个目标: -

function loading() {
        function nameBox() {
            var bigBox = document.getElementById("puzzlearea");
            var divs = bigBox.getElementsByTagName("div");
            for (var i = 0; i < divs.length; i++){
                divs[i].style.cssFloat = "left";
                divs[i].style.width = "19%";
                divs[i].style.border = "1px solid #ccc";
                divs[i].style.textAlign="center";
            }
        }
        nameBox();
    }
loading();

是JSFIDDLE D3}}