Html <div style display="none" > 表格内不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17461411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:44:46  来源:igfitidea点击:

<div style display="none" > inside a table not working

htmlcssdom

提问by Contemplating

I am trying to toggle display of a div element which has a label and a textbox using javascript. Here is the code snippet

我正在尝试使用 javascript 切换具有标签和文本框的 div 元素的显示。这是代码片段

<table id="authenticationSetting" style="display: none">
<div id="authenticationOuterIdentityBlock" style="display: none;">
                <tr>

                    <td class="orionSummaryHeader"><orion:message key="policy.wifi.enterprise.authentication.outeridentitity"/>: </td>
                    <td class="orionSummaryColumn">
                        <orion:textbox id="authenticationOuterIdentity" size="30"/>
                    </td>

                </tr>
                </div>

            </table>

However on page load the div element still displays ,the display toggling for the table element is working fine. I am at a loss as to why this is not working,could it be that the style of the table element is overriding the style of the div element. P.S. I am still able to hide elements inside the div but not the div itself.

但是在页面加载时,div 元素仍然显示,表格元素的显示切换工作正常。我不知道为什么这不起作用,可能是表格元素的样式覆盖了 div 元素的样式。PS 我仍然能够隐藏 div 内的元素,但不能隐藏 div 本身。

回答by aifarfa

simply change <div>to <tbody>

只需更改<div><tbody>

<table id="authenticationSetting" style="display: none">
  <tbody id="authenticationOuterIdentityBlock" style="display: none;">
    <tr>
      <td class="orionSummaryHeader">
        <orion:message key="policy.wifi.enterprise.authentication.outeridentitity" />:</td>
      <td class="orionSummaryColumn">
        <orion:textbox id="authenticationOuterIdentity" size="30" />
      </td>
    </tr>
  </tbody>
</table>

回答by Mr. Alien

Semantically what you are trying is invalid html, tableelement cannot have a divelement as a direct child. What you can do is, get your divelement inside a tdelement and than try to hide it

从语义上讲,您正在尝试的是无效的 html,table元素不能将div元素作为直接子元素。你可以做的是,把你的div元素放在一个td元素中,而不是试图隐藏它