【问题标题】:<optgroup label='-------'></optgroup> gives xhtml validation error<optgroup label='-------'></optgroup> 给出 xhtml 验证错误
【发布时间】:2013-10-18 03:56:48
【问题描述】:
Error: End tag for 'optgroup' which is not finished. You have probably failed to 
include a required child element. Hence the parent element is "not finished",
 not complete. 

我想在选择选项中实现类似的效果。

USA
UK
--
Afghanistan

我想把几个重要的国家放在最上面,然后是一个不可选择的分隔符,然后是剩余国家的有序列表。

我使用空的“optgroup”放置了这个分隔符。虽然它在所有浏览器中都能完美运行,但我收到验证错误。

还有什么其他方法可以实现这一点?

【问题讨论】:

    标签: html css optgroup


    【解决方案1】:

    与@ZippyV 写的类似,您可以使用&lt;option&gt; 并将其禁用:

    <option disabled='disabled'>--</option>
    

    这将无法选择。另外,如果是我,我会使用 m-dash 而不是两个连字符:

    <option disabled='disabled'>&mdash;</option>
    

    【讨论】:

    • “语义上有效”——这是什么意思?它将验证,因为“禁用”是&lt;option&gt; 标签的有效 XHTML(严格,偶数)属性。
    【解决方案2】:

    你没有发布任何代码,但我敢打赌你有类似的东西:

    <select>
        <option>USA</option>
        <option>UK</option>
        <optgroup label="---"></optgroup>
        <option>Afghanistan</option>
        <option>...</option>
    </select>
    

    这是无效的,因为您的 optgroup 不包含选项元素。你需要使用类似的东西:

    <select>
        <option>USA</option>
        <option>UK</option>
        <optgroup label="---">
            <option>Afghanistan</option>
            <option>...</option>
        </optgroup>
    </select>
    

    【讨论】:

    • 这会抵消 optgroup 选项。
    【解决方案3】:

    不要为此使用 optgroup。 只需将此&lt;option val="-"&gt;--&lt;/option&gt; 放入您的代码中

    【讨论】:

    • @SLaks 如果某个选项被禁用,则它是不可选择的。现在,我不知道相关XHTML DTD中的“禁用”属性是否可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多