简体   繁体   English

更改 innerHTML 会不必要地影响 div 尺寸

[英]Altering innerHTML is unwantedly affecting div dimensions

So I was writing this basic tictactoe game, and in the process I've made a grid using a container (display:flex) and 9 elements with class of 'cell' inside them.所以我正在编写这个基本的tictactoe游戏,在这个过程中,我使用一个容器(display:flex)和9个元素制作了一个网格,其中包含class的“单元格”。 Now I want to update the innerHTML of the div that gets clicked.现在我想更新被点击的 div 的 innerHTML。 But when I do so, the size of the div gets a little larger.但是当我这样做时, div 的大小会变大一些。 I've seen other people ask this question before in here too, like this Div element moves when changing the innerHTML .我以前也在这里看到过其他人问过这个问题,比如这个Div 元素在更改 innerHTML 时会移动 They all suggest using vertical-align: top/bottom;他们都建议使用 vertical-align: top/bottom; to the main container.到主容器。

I've tried doing this but that doesn't seem to work, in my case.我试过这样做,但在我的情况下这似乎不起作用。 When all the divs are filled up with text, the dimensions go back to normal again.当所有的 div 都被文本填满时,尺寸 go 再次恢复正常。 SO it's definitely a problem with empty divs only .所以这绝对是空 div 的问题 Any help?有什么帮助吗?

 function game(e) { const local = document.getElementById(`${e.id}`); local.innerHTML = 'X'; console.log(e.id); }
 body { margin: 0; padding: 0; } #container { width: 420px; height: 420px; display: flex; flex-wrap: wrap; background: pink; margin-top: 20px; margin-left: 20%; vertical-align: top; }.cell { flex: 1; margin: 6px; min-width: 30%; max-height: 30%; background: white; }
 <div id='container'> <div id='1' class='cell' onClick='game(this)'></div> <div id='2' class='cell' onClick='game(this)'></div> <div id='3' class='cell' onClick='game(this)'></div> <div id='4' class='cell' onClick='game(this)'></div> <div id='5' class='cell' onClick='game(this)'></div> <div id='6' class='cell' onClick='game(this)'></div> <div id='7' class='cell' onClick='game(this)'></div> <div id='8' class='cell' onClick='game(this)'></div> <div id='9' class='cell' onClick='game(this)'></div> </div>

I also tried adding a wide space to all the elements just so that they aren't empty anymore.我还尝试为所有元素添加一个宽阔的空间,以使它们不再为空。

for(let i=1; i<10;i++)
  document.getElementById(`${i}`).innerHTML=' ';

That didn't help either, and moreover I don't exactly prefer using this even if it would have worked.这也无济于事,而且即使它会起作用,我也不完全喜欢使用它。 Looking for a seamless solution.寻找无缝解决方案。

Note: I've been doing html and javascript for 2 months only, so not exactly superior with coding and/or syntax.注意:我只做了 2 个月的 html 和 javascript,所以在编码和/或语法方面并不完全优越。 Any criticism would be helpful.任何批评都会有所帮助。

Here you don't need your cells to change size, you want them to be 30% of the grid (in height and width) all the time.在这里,您不需要更改单元格的大小,您希望它们始终占网格的 30%(高度和宽度)。

Just use width and height instead of max-width and min-width .只需使用widthheight而不是max-widthmin-width

And remove flex: 1;并删除flex: 1;

 function game(e) { const local = document.getElementById(`${e.id}`); local.innerHTML = 'X'; console.log(e.id); }
 body { margin: 0; padding: 0; } #container { width: 420px; height: 420px; display: flex; flex-wrap: wrap; background: pink; margin-top: 20px; margin-left: 20%; vertical-align: top; }.cell { margin: 6px; width: 30%; height: 30%; background: white; }
 <div id="container"> <div id="1" class="cell" onClick="game(this)"></div> <div id="2" class="cell" onClick="game(this)"></div> <div id="3" class="cell" onClick="game(this)"></div> <div id="4" class="cell" onClick="game(this)"></div> <div id="5" class="cell" onClick="game(this)"></div> <div id="6" class="cell" onClick="game(this)"></div> <div id="7" class="cell" onClick="game(this)"></div> <div id="8" class="cell" onClick="game(this)"></div> <div id="9" class="cell" onClick="game(this)"></div> </div>

One possible fix would be to put in a &nbsp;一种可能的解决方法是放入&nbsp; in each at the start在每个开始

 function game(e) { const local = document.getElementById(`${e.id}`); local.innerHTML = 'X'; console.log(e.id); }
 body { margin: 0; padding: 0; } #container { width: 420px; height: 420px; display: flex; flex-wrap: wrap; background: pink; margin-top: 20px; margin-left: 20%; vertical-align: top; }.cell { flex: 1; margin: 6px; min-width: 30%; max-height: 30%; background: white; }
 <div id='container'> <div id='1' class='cell' onClick='game(this)'>&nbsp;</div> <div id='2' class='cell' onClick='game(this)'>&nbsp;</div> <div id='3' class='cell' onClick='game(this)'>&nbsp;</div> <div id='4' class='cell' onClick='game(this)'>&nbsp;</div> <div id='5' class='cell' onClick='game(this)'>&nbsp;</div> <div id='6' class='cell' onClick='game(this)'>&nbsp;</div> <div id='7' class='cell' onClick='game(this)'>&nbsp;</div> <div id='8' class='cell' onClick='game(this)'>&nbsp;</div> <div id='9' class='cell' onClick='game(this)'>&nbsp;</div> </div>

Since you are using a "grid" you can also use css grid , visually you can set the sizes as you wish here but this is an example of that:由于您使用的是“网格”,因此您也可以使用 css grid ,您可以在视觉上根据需要设置大小,但这是一个示例:

 const rootElement = document.getElementById('container'); const containerChars = JSON.parse(rootElement.dataset.chars); const cells = rootElement.getElementsByClassName('cell'); function game() { let c = rootElement.dataset.charLast == containerChars[0]? containerChars[1]: containerChars[0]; rootElement.dataset.charLast = c; this.innerHTML = c; } for (let i = 0; i < cells.length; i++) { cells[i].onclick = game; }
 body { margin: 0; padding: 0; } #container { border: solid 2px #dddddd; display: grid; grid-template-columns: repeat(3, minmax(2em, 1fr)); grid-template-rows: repeat(3, minmax(2em, 1fr)); background: #FFCCDD; grid-gap: 0.25rem; }.cell { background: #ddEEFF; display: flex; justify-content: center; align-items: center; font-size: 1.75em; }
 <div id='container' data-char-last="X" data-chars='["X","O"]'> <div id='1' class='cell'></div> <div id='2' class='cell'></div> <div id='3' class='cell'></div> <div id='4' class='cell'></div> <div id='5' class='cell'></div> <div id='6' class='cell'></div> <div id='7' class='cell'></div> <div id='8' class='cell'></div> <div id='9' class='cell'></div> </div>

Your problem css.您的问题 css。 Try...尝试...

.cell {
    /* flex: 1; */
    /* margin: 6px; */
    min-width: 32.9%;
    /* max-height: 30%; */
    background: white;
    border: 1px solid;
    height: 32.9%;
}

try adding this line to your css:尝试将此行添加到您的 css:

* {
    box-sizing: border-box;
}

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

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