设置假值与移除属性 [英] Setting false value vs. removing an attribute

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

问题描述

我读到一些关于布尔属性这里,它说,对于一个布尔属性(在这个特殊的例子,循环属性<音频> ),可以设定任何值,它会被确认为真。为了真正设为falsy,则不能设置像环路= FALSE 或JavaScript作为 ['循环'] = FALSE ,但必须删除的属性,如通过做的removeAttribute('循环')。这是真的吗?

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检查它,它似乎是设置为 ['循环'] = FALSE 将其实也使其被视为falsy。我不知道这个事实如何强大的是考虑跨browserly时。是否有浏览器之间有什么区别?

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 /前奏/ 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").

所以,虽然一些浏览器可以跨preT字符串假,如果没有设置值,其他人可能不会决定(这是正确的行为)。其实,据我所知(或认为的),任何非空字符串通常将该值设置为ON /真(无论什么样的规范说是合法值)。我相信这也是不确定的行为,因此这可能会改变,以及或浏览器是不同的浏览器(不依赖于它)。

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 = FALSE ATTR =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 -->

(上有他们的元素)的所有属性值被视为字符串。换句话说,有一个真正的布尔没有这样的事情的的(或 NULL 值),HTML像有在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天全站免登陆