设置错误值与删除属性
我正在阅读有关布尔属性此处的内容,其中表示对于布尔属性(在此具体示例, 的
loop
属性),无论您设置什么值,它都会被识别为“true”。为了真正设置为falsy,你不能像loop=false
那样设置它,也不能用javascript设置['loop']=false
,而是必须删除诸如通过执行removeAttribute('loop')
。这是真的吗?
我一开始相信了这一点,但据 Chrome 进行检查,似乎设置为 ['loop']=false
实际上确实会使其被识别为虚假。我不确定这个事实在跨浏览器考虑时有多稳健。浏览器之间有什么区别吗?
I was reading something about boolean attribute here, which says that for a boolean attribute (in this particular example, loop
attribute of <audio>
), whatever value you set, it is going to be recognized as "true". In order to really set to falsy, you cannot set it like loop=false
or with javascript as ['loop']=false
, but have to remove the attribute such as by doing removeAttribute('loop')
. Is this true?
I first believed it, but as far as checked it with Chrome, it seems that setting to ['loop']=false
will actually do make it be recognized as falsy. I am not sure how robust this fact is when considered cross-browserly. Is there any difference among browsers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
布尔属性解释如下:
http://www.w3 .org/TR/html4/intro/sgmltut.html#h-3.3.4.2
因此,虽然某些浏览器可能会将字符串“false”解释为未设置该值,但其他浏览器可能不会决定这样做(这是正确的行为)。实际上,据我所知(或想法),任何非空字符串通常将值设置为 on/true (无论规范所说的合法值是什么)。我相信这也是未定义的行为,因此这也可能会随着浏览器的不同而改变或有所不同(不要依赖它)。
最重要的是,仅仅因为一两个浏览器可能偏离规范并不意味着您应该这样做。完全删除该属性是正确的方法。
附录:仔细看看您的评论和问题,我认为您可能对一般属性值感到困惑。在 HTML 中,
attr=false
和attr="false"
完全相同。任何版本的 HTML 中都不需要引号(除非当值包含空格时需要使用引号来消除歧义)。例如:所有属性值(在具有这些属性的元素上)都被视为字符串。换句话说,HTML 中不存在像 JavaScript 中那样的真正的布尔值(或 NULL 值)。
Boolean attributes are explained here:
http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2
So, while some browsers may interpret the string "false" as if the value was not set, others may not decide to (which is the correct behavior). Actually, as far as I know (or thought), any non-empty string usually sets the value to on/true (regardless of what the spec says is a legal value). I believe this is also undefined behavior, so this may change as well or be different from browser to browser (don't rely on it).
The bottom line is, just because a browser or two may deviate from the spec doesn't mean that you should. Removing the attribute entirely is the way to go.
Addendum: Looking at your comments and question a little closer, I think you may be confused about attribute values in general. In HTML,
attr=false
andattr="false"
are exactly the same. Quotes are not required in any version of HTML (unless they are needed to remove ambiguity when the value contains spaces). For instance:All attribute values (on elements that have them) are treated as strings. In other words, there is no such thing as a true boolean value (or
NULL
value) in HTML like there is in javascript.因此,将来需要此功能的任何人:
除非删除整个
loop
属性,否则loop=false
仍保持true
。基本上,只要存在循环
,标签就需要做其他事情。您需要使用类似 jQuery 的东西来删除整个loop
属性(或者至少我会这样做)。现在,如果您将另一个未定义属性设置为false
,那么您就能够将其识别为false
。Just so anyone who needs this in the future:
loop=false
remainstrue
unless the entireloop
attribute is removed. Basically, the presence of justloop
is what a tag needs to do something else. You need to use something like jQuery to remove the entierloop
attribute (or at least that's what I would do). Now, if you set a different undefined attribute tofalse
, then you are able to recognize it asfalse
.audio
元素是一个 HTML5 元素,因此有关其含义,您应该查阅 HTML5 草案。在这种情况下,请参阅开发者版本中的布尔属性的定义 WHATWG 草案。实际上,它表示:a) 属性的存在或不存在决定了 DOM 属性值是true
还是false
,b) 作为对文档的要求,该值必须为空或(不区分大小写)属性名称,在本例中为loop=''
或loop="loop"
。值周围引号的使用在其他地方定义。因此,浏览器需要识别
loop=false
与loop=loop
或loop=true
的含义相同,但作者不得使用此类构造,HTML5 检查器会发出有关它们的错误消息。(基本上,您应该在 HTML5 的 HTML 序列化中使用
loop
,在 XHTML 序列化中使用loop="loop"
。)因此,如果您有一个变量
x
在 JavaScript 中,以audio
元素对象作为其值,x.loop
的值为true
或false而
x.attributes['loop'].value
表示 HTML 标记中使用的值(通常并不有趣)。对于 Firefox,还有一个更复杂的问题:它似乎仍然不支持
loop
属性(请参阅问题 HTML5 音频循环)。这意味着,如果您设置loop="loop"
,x.attributes['loop'].value
将是loop
,但 Firefox 会这样做甚至没有设置x.loop(即,它是未定义的),更不用说实现该功能了。The
audio
element is an HTML5 element, so regarding its meaning, you should consult the HTML5 drafts. In this case, see the definition of boolean attributes in the developer version of the WHATWG draft. It says, in effect, a) that the presence or absence of the attribute determines whether the DOM attribute value istrue
orfalse
, and b) as a requirement on documents, the value must be empty or (case-insensitively) the attribute name, in this caseloop=''
orloop="loop"
. The use of quotation marks around the value are defined elsewhere.Thus, browsers are required to recognize
loop=false
to mean the same asloop=loop
orloop=true
, but authors must not use such constructs, and HTML5 checkers issue error messages about them.(Basically, you’re supposed to use just
loop
in HTML serialization of HTML5 andloop="loop"
in XHTML serialization.)Thus, if you have a variable
x
in JavaScript with anaudio
element object as its value,x.loop
has the valuetrue
orfalse
whereasx.attributes['loop'].value
indicates the value used in HTML markup (which is usually not interesting as such).There’s a further complication as regards to Firefox: it still does not seem to support the
loop
attribute (see the question HTML5 Audio Looping). This means that if you set e.g.loop="loop"
,x.attributes['loop'].value
will beloop
but Firefox does not even setx.loop
(i.e., it isundefined
), still less implement the functionality.您混淆了字符串和真正的布尔类型。 Javascript 有一个布尔数据类型,有两个可能的值 true 和 false(不带引号)。字符串可以包含任何文本,因此它们可以包含带引号的“true”和“false”。将属性设置为非 null 和非 false 会产生 true,因此将遵循以下内容:
注意与 C 的相似之处。
You're confusing strings and real Boolean types. Javascript has a Boolean datatype with two possible values of true and false (without quotes). Strings can contain any text, so can they contain "true" and "false" with quotes. Setting a property to non-null and non-false yields true, so the following will accour:
Note the similarities with C.