在HTML标记中是否允许ID为空? [英] Is an ID allowed to be empty in an HTML tag?

查看:316
本文介绍了在HTML标记中是否允许ID为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div id =>< p< p>< ; / DIV> 

我使用了一个模板,其中元素的ID由一个变量有一个值,我想知道这是否会导致任何不可预见的问题。编辑:变量实际上是一个Django模板中的可编辑区域(块)。这意味着我不能在其上放置任何条件逻辑。

 < div id ={%block id%} { %endblock%}> 

而不是:

 < div {%block id%} {%endblock%}> 

必须在子模板中继承:

  {%block id%} id =whatever{%endblock%} 


解决方案

根据W3C: b
$ b


ID和名称标记必须以
字母([A-Za-z])开头,可以用任意数量的字母,数字
([0-9]),连字符( - ),强调
(_),冒号(:)和句点
(。)。

(来源:基本HTML数据类型

这表明根据上述定义,该属性的空白值无效。



尝试根据空的ID属性进行验证会返回以下内容:


属性值的语法不符合
符合声明的值


< div id => something< / div>



属性的值包含
这是
指定语法对于
属性的类型所不允许的。例如,
selected属性必须是
最小化为selected或拼出完整为
为selected =selected;
变体selected =是不允许的。



Is the ID attribute on an HTML element allowed to be empty?

<div id=""></div>

I'm using a template where the ID of the element is set by a variable which may not always have a value and I'm wondering if this could cause any unforeseen problems.

EDIT: The "variable" is actually an editable region (block) in a Django template. This means I can't put any conditional logic on it.

<div id="{% block id %}{% endblock %}">

Rather than:

<div {% block id %}{% endblock %}>

Which would have to be inherited in the sub-template as:

{% block id %}id="whatever"{% endblock %}

解决方案

According to the W3C:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

(Source: Basic HTML data types)

This would indicate that a blank value for the attribute is not valid in accordance with the above definition.

Trying to validate against an empty ID attribute returns the following:

syntax of attribute value does not conform to declared value

<div id="">something</div>

The value of an attribute contained something that is not allowed by the specified syntax for that type of attribute. For instance, the "selected" attribute must be either minimized as "selected" or spelled out in full as "selected="selected""; the variant "selected=""" is not allowed.

这篇关于在HTML标记中是否允许ID为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆