设置错误值与删除属性

发布于 2024-12-25 09:50:35 字数 457 浏览 1 评论 0原文

我正在阅读有关布尔属性此处的内容,其中表示对于布尔属性(在此具体示例,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盗梦空间 2025-01-01 09:50:35

布尔属性解释如下:

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

一些属性扮演布尔变量的角色(例如,选定的
OPTION 元素的属性)。它们出现在开始标签中
元素的 意味着该属性的值为“true”。他们的
缺席意味着值为“false”。

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

因此,虽然某些浏览器可能会将字符串“false”解释为未设置该值,但其他浏览器可能不会决定这样做(这是正确的行为)。实际上,据我所知(或想法),任何非空字符串通常将值设置为 on/true (无论规范所说的合法​​值是什么)。我相信这也是未定义的行为,因此这也可能会随着浏览器的不同而改变或有所不同(不要依赖它)。

最重要的是,仅仅因为一两个浏览器可能偏离规范并不意味着您应该这样做。完全删除该属性是正确的方法。

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

<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 中不存在像 JavaScript 中那样的真正的布尔值(或 NULL 值)。

Boolean attributes are explained here:

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

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".

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

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 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 -->

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.

り繁华旳梦境 2025-01-01 09:50:35

因此,将来需要此功能的任何人:

除非删除整个 loop 属性,否则 loop=false 仍保持 true 。基本上,只要存在循环,标签就需要做其他事情。您需要使用类似 jQuery 的东西来删除整个 loop 属性(或者至少我会这样做)。现在,如果您将另一个未定义属性设置为 false,那么您就能够将其识别为 false

Just so anyone who needs this in the future:

loop=false remains true unless the entire loop attribute is removed. Basically, the presence of just loop is what a tag needs to do something else. You need to use something like jQuery to remove the entier loop attribute (or at least that's what I would do). Now, if you set a different undefined attribute to false, then you are able to recognize it as false.

舞袖。长 2025-01-01 09:50:35

audio 元素是一个 HTML5 元素,因此有关其含义,您应该查阅 HTML5 草案。在这种情况下,请参阅开发者版本中的布尔属性的定义 WHATWG 草案。实际上,它表示:a) 属性的存在或不存在决定了 DOM 属性值是 true 还是 false,b) 作为对文档的要求,该值必须为空或(不区分大小写)属性名称,在本例中为 loop=''loop="loop"。值周围引号的使用在其他地方定义。

因此,浏览器需要识别 loop=falseloop=looploop=true 的含义相同,但作者不得使用此类构造,HTML5 检查器会发出有关它们的错误消息。

(基本上,您应该在 HTML5 的 HTML 序列化中使用 loop ,在 XHTML 序列化中使用 loop="loop" 。)

因此,如果您有一个变量 x 在 JavaScript 中,以 audio 元素对象作为其值,x.loop 的值为 truefalse而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 is true or false, and b) as a requirement on documents, the value must be empty or (case-insensitively) the attribute name, in this case loop='' or loop="loop". The use of quotation marks around the value are defined elsewhere.

Thus, browsers are required to recognize loop=false to mean the same as loop=loop or loop=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 and loop="loop" in XHTML serialization.)

Thus, if you have a variable x in JavaScript with an audio element object as its value, x.loop has the value true or false whereas x.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 be loop but Firefox does not even set x.loop (i.e., it is undefined), still less implement the functionality.

梦忆晨望 2025-01-01 09:50:35

您混淆了字符串和真正的布尔类型。 Javascript 有一个布尔数据类型,有两个可能的值 true 和 false(不带引号)。字符串可以包含任何文本,因此它们可以包含带引号的“true”和“false”。将属性设置为非 null 和非 false 会产生 true,因此将遵循以下内容:

var a = true; // true
var b = false; // false
var c = "true"; // true
var d = "false" // true
var e = null; // false;
var f = 0; // false
var g = 1; // 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:

var a = true; // true
var b = false; // false
var c = "true"; // true
var d = "false" // true
var e = null; // false;
var f = 0; // false
var g = 1; // true

Note the similarities with C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文