简体   繁体   English

如何给Div分配具体职位?

[英]How do I give a Div the specific position?

I have a Button Onclick Function which makes a box appear when clicked on. 我有一个按钮Onclick函数,单击该函数可使一个框出现。

I want that box to appear at a certain place, and since i have 2 tables i cant give it positions or something since the position will be different cuz the tables are next to each other. 我希望该框出现在某个位置,并且由于我有2张桌子,所以我无法为其指定位置或其他位置,因为该位置会有所不同,因为桌子彼此相邻。 And somehow the second table is a bit lower then the other one. 而且第二张桌子要比另一张桌子低一些。 can someone help me with that aswell please? 有人可以帮我吗?

Here's my full code. 这是我的完整代码。


 function footafel(id){ for(var i=1; i<=13; i++){ document.getElementById('tafel'+i).className = 'hide'; } if(id == ''){ return false; } else { document.getElementById('tafel'+id).className = 'show'; } } 
 div { width: 50%; height: 50px; border: solid 1px black; } button { background-color: white; border: solid 2px lightblue; width: 150px; height: 60px; margin: 10px; text-align: center; line-height: 56px; } #tablerechts { position: relative; bottom: 40px; } table { margin-left: 25px; width: 300px; } .hide { display: none; } .show { display: block; } 
 <table> <tr> <td id="tablelinks"> <div id="tafel1" class="hide">Tafel 1</div> <div id="tafel2" class="hide">Tafel 2</div> <div id="tafel3" class="hide">Tafel 3</div> <div id="tafel4" class="hide">Tafel 4</div> <div id="tafel5" class="hide">Tafel 5</div> <div id="tafel6" class="hide">Tafel 6</div> <div id="tafel7" class="hide">Tafel 7</div> <button onclick="footafel('1')">Tafel oefening 1</button> <button onclick="footafel('2')">Tafel oefening 2</button> <button onclick="footafel('3')">Tafel oefening 3</button> <button onclick="footafel('4')">Tafel oefening 4</button> <button onclick="footafel('5')">Tafel oefening 5</button> <button onclick="footafel('6')">Tafel oefening 6</button> <button onclick="footafel('7')">Tafel oefening 7</button> </td> <td id="tablerechts"> <div id="tafel8" class="hide">Tafel 8</div> <div id="tafel9" class="hide">Tafel 9</div> <div id="tafel10" class="hide">Tafel 10</div> <div id="tafel11" class="hide">Tafel 11</div> <div id="tafel12" class="hide">Tafel 12</div> <div id="tafel13" class="hide">Tafel 13</div> <button onclick="footafel('8')">Tafel oefening 8</button> <button onclick="footafel('9')">Tafel oefening 9</button> <button onclick="footafel('10')">Tafel oefening 10</button> <button onclick="footafel('11')">Tafel oefening 11</button> <button onclick="footafel('12')">Tafel oefening 12</button> <button onclick="footafel('13')">Tafel oefening 13</button> </td> </table> 

Thanks in advance. 提前致谢。

You forgot to close the <tr> , that's why your tables are not aligned. 您忘记了关闭<tr> ,这就是表格未对齐的原因。 That being said, I ill advise you to use table this way. 话虽如此,我不建议您这样使用表。 The use of tables are in decline, and they must be used if what you want to display is really a table, with a header, and lines, like an excel sheet for example. 表格的使用在减少,如果您要显示的实际上是具有标题和行的表格(例如excel表格),则必须使用表格。

I suggest you separating the buttons from what they will display, putting the buttons in a separate div, and the content that must appear, in another div at the top of it, this will prevent the columns from desalign. 我建议您将按钮与将要显示的按钮分开,将按钮放在单独的div中,并将必须显示的内容放在顶部的另一个div中,这样可以防止列不对齐。

I used display: flex in each "table" with flex-flow: column to get the desired "table" effect, and also display: flex on the parent to keep them aligned, which is a feature supported by every browser already. 我在每个“表”中使用display: flexflex-flow: column来获得所需的“表”效果,并在父级上display: flex以保持它们对齐,这是每个浏览器都支持的功能。 I also suggest an improvement to your script, by taking only the <divs> which has the .show class and giving them the hide class, insted of hard codding a specific number, which makes your page able to scale to any number of buttons you want. 我还建议对脚本进行改进,方法是只采用具有.show类的<divs>并为它们提供hide类,以硬编码一个特定的数字为例,这使您的页面可以缩放到任意数量的按钮想。

Hope it helps. 希望能帮助到你。

 function footafel(id){ var allDivsToHide = document.getElementsByClassName('show'); for(var i=0; i < allDivsToHide.length; i++){ allDivsToHide[i].className = 'hide'; } if(id == ''){ return false; } else { document.getElementById('tafel' + id).className = 'show'; } } 
 .table { display: flex; flex-flow: column; } button { background-color: white; border: solid 2px lightblue; width: 150px; height: 60px; margin: 10px; text-align: center; line-height: 56px; } .hide { display: none; } .show { display: block; } 
 <div style="display: flex;"> <div id="tablelinks" class="table"> <button onclick="footafel('1')">Tafel oefening 1</button> <button onclick="footafel('2')">Tafel oefening 2</button> <button onclick="footafel('3')">Tafel oefening 3</button> <button onclick="footafel('4')">Tafel oefening 4</button> <button onclick="footafel('5')">Tafel oefening 5</button> <button onclick="footafel('6')">Tafel oefening 6</button> <button onclick="footafel('7')">Tafel oefening 7</button> </div> <div id="tablerechts" class="table"> <button onclick="footafel('8')">Tafel oefening 8</button> <button onclick="footafel('9')">Tafel oefening 9</button> <button onclick="footafel('10')">Tafel oefening 10</button> <button onclick="footafel('11')">Tafel oefening 11</button> <button onclick="footafel('12')">Tafel oefening 12</button> <button onclick="footafel('13')">Tafel oefening 13</button> </div> <div id="tablelinks-content" class="table"> <div id="tafel1" class="hide">Tafel 1</div> <div id="tafel2" class="hide">Tafel 2</div> <div id="tafel3" class="hide">Tafel 3</div> <div id="tafel4" class="hide">Tafel 4</div> <div id="tafel5" class="hide">Tafel 5</div> <div id="tafel6" class="hide">Tafel 6</div> <div id="tafel7" class="hide">Tafel 7</div> </div> <div id="tablerechts-content" class="table"> <div id="tafel8" class="hide">Tafel 8</div> <div id="tafel9" class="hide">Tafel 9</div> <div id="tafel10" class="hide">Tafel 10</div> <div id="tafel11" class="hide">Tafel 11</div> <div id="tafel12" class="hide">Tafel 12</div> <div id="tafel13" class="hide">Tafel 13</div> </div> </div> 

There are two ways to do this. 有两种方法可以做到这一点。

The first method uses the style attribute and is generally frowned upon as you want to keep all your styling in your CSS file. 第一种方法使用style属性,当您希望将所有样式保留在CSS文件中时,通常会皱眉。 Example: 例:

 <div id="tafel1" class="hide" style="position:absolute;top:200px;">Tafel 1</div>

You can also use position:relative; 您还可以使用position:relative; to position the div relative to the element that contains it (in this case it would be the table that it's inside). 将div相对于包含它的元素定位(在这种情况下,它将是它所在的表)。

The second way is to go into your CSS and style the div from there. 第二种方法是进入CSS并从那里设置div的样式。

You can also target your tafel1 div in the CSS like this: 您还可以像这样在CSS中定位tafel1 div:

#tafel1{
    position:absolute;
    top:200px;
}

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

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