设置错误值与删除属性 [英] Setting false value vs. removing an attribute

查看:16
本文介绍了设置错误值与删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 here 阅读了一些关于布尔属性的内容,它表示布尔属性(在这个特定的例子中,<audio>loop 属性),无论你设置什么值,它都会被识别为真".为了真正设置为 falsy,您不能将其设置为 loop=false 或将 javascript 设置为 ['loop']=false,而必须删除诸如通过执行 removeAttribute('loop').这是真的吗?

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?

我一开始是相信的,但就 Chrome 的检查而言,似乎设置为 ['loop']=false 实际上确实会使其被识别为虚假.当考虑跨浏览器时,我不确定这个事实有多强大.浏览器有区别吗?

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?

推荐答案

这里解释布尔属性:

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

一些属性扮演布尔变量的角色(例如,选定的OPTION 元素的属性).他们在开始标签中的出现元素的值意味着属性的值为真".他们的缺席意味着值为假".

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

布尔属性可以合法地采用单个值:属性本身(例如,selected="selected").

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

因此,虽然某些浏览器可能会将字符串false"解释为好像没有设置值,但其他浏览器可能不会决定(这是正确的行为).实际上,据我所知(或认为),任何非空字符串通常会将值设置为 on/true(无论规范中所说的合法​​值如何).我相信这也是未定义的行为,因此这也可能会因浏览器而异或有所不同(不要依赖它).

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.

附录:仔细观察您的评论和问题,我想您可能会对一般的属性值感到困惑.在 HTML 中,attr=falseattr="false" 完全相同.在任何版本的 HTML 中都不需要引号(除非在值包含空格时需要引号来消除歧义).例如:

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 and attr="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:

<input class=required>
<!-- This is fine -->

<input class=title required>
<!-- this is fine too, but "required" will be parsed as an attribute -->

<input class="title required">
<!-- To have two classes, we need the quotes -->

所有属性值(在具有它们的元素上)都被视为字符串.换句话说,在 HTML 中没有真正的布尔 value(或 NULL 值),就像在 javascript 中那样.

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.

这篇关于设置错误值与删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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