简体   繁体   English

如何在中间对齐 DocumentFragment?

[英]How do I align my DocumentFragment in the middle?

I have created a set of buttons 9x9 but they are in the left upper corner and I would like to align them all in the center.我创建了一组 9x9 的按钮,但它们位于左上角,我想将它们全部对齐在中心。 I have tried using <div> to align it all, but it doesn't work and nothing appears on the page.我曾尝试使用<div>来对齐所有内容,但它不起作用,并且页面上没有任何内容。

    var docFrag = document.createDocumentFragment();
    for (var i=0; i < 9 ; i++){ 
        var row = document.createElement("tr") 
        for (var j=0; j < 9 ; j++){ 
                var elem = document.createElement('input');
                elem.className = 'gumb';
                elem.type = 'button';
                ...something...
                docFrag.appendChild(elem); 
            } 
        document.body.appendChild(docFrag); 
        document.body.appendChild(row); 
    } 

Use this js instead:改用这个js:

// use div to add class to center the elements
var item = document.createElement('div');
item.className = "container";

    for (var i=0; i < 9 ; i++){
        var docFrag = document.createDocumentFragment();
        var row = document.createElement("br") 
        for (var j=0; j < 9 ; j++){ 
                var elem = document.createElement('input');
                elem.className = 'gumb';
                elem.type = 'button';
                docFrag.appendChild(elem); 
            } 
        item.appendChild(docFrag); 
        item.appendChild(row); 
    } 
document.body.appendChild(item)
// make the `container` div full width and center the items in it
.container {
     width: 100%;
     text-align:center;
}

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

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