【问题标题】:display: none does not work inside option tag in htmldisplay: none 在 html 中的选项标签内不起作用
【发布时间】:2018-11-14 13:32:17
【问题描述】:

display: none; 在 html 的选项标签内不起作用。为什么?

html

<select>

<option data-color="green"> Option 1 
<div style="display: none;"> hiddenText </div>
</option>
<option data-color="red"> Option 1
</option>

</select>

jsfiddle

我将 style="display: none;" 应用于 textContent 等于 hiddenText 的节点。所以,我预计不会看到hiddenText,但我仍然看到它。我在这里错过了什么?

谢谢。

如果我们将 div inside 选项更改为 span,它仍然不起作用。

【问题讨论】:

标签: html css select option display


【解决方案1】:

我认为你不能这样做。 &lt;option&gt; 标签不能包含任何其他标签。

尝试使用一些库,例如Select2

【讨论】:

    【解决方案2】:

    HTML 标签在选项标签内被忽略。 w3

    【讨论】:

      【解决方案3】:

      在网络浏览器中呈现视图后,选择文本不接受选项标签内的任何 div,无论您在选项标签中写入什么,它都会假定为文本

      如果你检查你的 html 你会看到什么 html 部分进入视图是这样的 这就是为什么您的 div 属性显示 none 不起作用的原因。

      <select>
      <option data-color="green"> Option 1 
       hiddenText 
      </option>
      <option data-color="red"> Option 1
      </option>
      </select>
      

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 2018-05-08
        • 2018-03-01
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多