繁体   English   中英

在表td中给divs类名

[英]give divs class name inside table td

我正试图给一堆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();
};

应该这样做,您遇到了许多语法错误,并且逻辑上有些失误,但是我将程序的整体格式保持不变:

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();
};

我对此不太确定,但也许您应该尝试将您的谜题区域和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';
}
}
}

然后继续其他功能,只需修改变量即可。

我认为您正在尝试达到以下目的:-

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();

这里是JSFIDDLED

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM