简体   繁体   English

HTML:使用JavaScript时,“表格”中的“ div”呈现怪异

[英]Html: “div” inside “table” renders weird when using javascript

I'm pretty new to html/javascript. 我是html / javascript的新手。 So please forgive my rookie question. 所以请原谅我的菜鸟问题。

Suppose I insert a div inside the table: 假设我在表中插入一个div

<table border="1">
  <tr><td>Cell-11</td><td>Cell-12</td></tr>
  <div>
    <tr><td>Cell-21</td><td>Cell-22</td></tr>
  </div>
</table>

The code renders fine 代码呈现良好

div1

However, if I use a script to fill the div 但是,如果我使用脚本来填充div

<table border="1">
  <tr><td>Cell-11</td><td>Cell-12</td></tr>
  <div id="fill"></div>
</table>
<script>
 document.getElementById("fill").innerHTML="<tr><td>Cell-21</td><td>Cell-22</td></tr>";
</script>

the table has a weird rendering: 该表有一个奇怪的渲染:

div2

Any idea why this is happening? 知道为什么会这样吗?

My goal here is to use div as an anchor so that I can fill in custom row(s) with javascript, and I don't want to use <tr id="fill"></tr> because I sometimes want to fill more than one rows. 我的目标是使用div作为锚点,以便我可以用javascript填充自定义行,并且我不想使用<tr id="fill"></tr>因为有时我想填充多行。 Is there a standard/recommended way of doing this? 是否有标准/推荐的方法? Thanks a lot! 非常感谢!


Thanks to Jake's answer, replacing <div> with <tbody> seems to allow me do what I have in mind. 感谢Jake的回答,用<div> <tbody>替换<div> <tbody>似乎可以让我做我想做的事情。

<table border="1">
  <tr><td>Cell-11</td><td>Cell-12</td></tr>
  <tbody id="fill"></tbody>
</table>
<script>
 document.getElementById("fill").innerHTML="<tr><td>Cell-21</td><td>Cell-22</td></tr>";
</script>

When I try this in jsfiddle, ( http://jsfiddle.net/318xjqsu/ ) and then view the source, I see that the manipulated DOM looks like: 当我在jsfiddle( http://jsfiddle.net/318xjqsu/ )中尝试此操作,然后查看源代码时,我看到操纵的DOM如下所示:

<div id="fill">Cell-21Cell-22</div><table border="1">
<tbody><tr><td>Cell-11</td><td>Cell-12</td></tr>

</tbody></table>

So you can see that the javascript is being inserted before the table. 因此,您可以看到在表之前插入了javascript。 I believe this is because it is invalid to have a <div> inside the <table> element. 我相信这是因为在<table>元素中包含<div>是无效的。 From the w3C: 从w3C:

the table element is now also alternatively allowed to contain a tfoot after any tbody or tr elements. 现在,也可以允许table元素在任何tbody或tr元素之后包含一个tfoot。 wc3 table reference wc3表参考

...implying that the table element cannot contain any other element. ...暗示表元素不能包含任何其他元素。

This is happening because you are violating the structure of table in your prev 发生这种情况是因为您违反了上一个表格的结构

 <div>
<tr><td>Cell-21</td><td>Cell-22</td></tr>
</div> 

you can not have tr inside div directly in a table u have to make another table inside div then use tr inside div, so whats happening is when you trying to parse tr inside div then html parser or your browser is correcting you structure automatically by taking div out of ur table you can check in firebug , 您不能在表中直接包含div内部的tr,而必须在div内部创建另一个表,然后在div内部使用tr,所以发生的情况是,当您尝试在div内部解析tr然后是html解析器时,或者您的浏览器通过采用您可以从div表中检出萤火虫,

Now in second example it not working previous because not ur html parser or browser will not correct the structure for you dynamically it will simply wont allow you to add tr inside div and remove bad element from the structure and place the data directly in div not in tr inside div 现在在第二个示例中,它以前无法正常工作,因为不是ur html解析器或浏览器无法动态地为您校正结构,它只是不允许您在div内添加tr并从结构中删除不良元素并将数据直接放在div中而不是div内部的tr

What if you saved the content of your table to a variable, add the rows you want to add. 如果将表的内容保存到变量中,该怎么办,添加要添加的行。 And then overwrite the innerHTML with said variable. 然后使用所述变量覆盖innerHTML。 That way you eliminate the use of a div there. 这样一来,您就无需在其中使用div了。

To clarify something like this: 为了澄清这样的事情:

<table id="table">
    <tr><td>Cell-11</td><td>Cell-12</td></tr>
</table>
<script>
    <!-- store the contents of your table -->
    var table = document.getElementById("table");
    var current = table.innerHTML;

    <!-- append the new cells -->
    current += "<tr><td>Cell-13</td><td>Cell-14</td></tr>";

    <!-- overwrite the innerHTML with the new data -->
    table.innerHTML = current;
</script>

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

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