为什么一些技术上可序列化的输入属性是可序列化的? [英] Why aren't some technically serializable input properties serializable?

查看:95
本文介绍了为什么一些技术上可序列化的输入属性是可序列化的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可序列化属性定义为:




  • 当您设置元素的属性时,它将反映在序列化查询中,例如 getAttribute ,您可以看到DOM检查器中的更改

  • 当您获得 .innerHTML 元素的parentnode,返回的html字符串将包含所有可序列化属性作为其属性对应



我已经创建了一个页面看起来它可靠地在Chrome和Firefox中打印输入元素的所有可序列化属性的表: http://jsfiddle.net/tEVLp/16/
自定义属性永远不可序列化,所以在firefox webkitSpeech 等是不可序列化的。



所有布尔值为 true ,因为 false的序列化属性将是没有属性,这是测试中的一个假阴性。



所以我的问题是,为什么不是属性 .value .checked serializable?



从技术上讲,两者都是可序列化的。 .value 只是一个字符串,浏览器没有序列化其他布尔属性的问题,例如 .readOnly .disabled



我最好的猜测是,由于 .defaultValue 序列化到value -attribute和 .defaultChecked 序列化到checked code> -attribute,会有冲突,因此
.value .checked 不能序列化。在这种情况下,为什么为这些选择了 defaultX ,而不是那些反映更有用的当前 .value .checked 状态?

解决方案

输入元素规范定义了确切的行为。开始阅读此处(之前那个DOM界面,属性和类型被定义)



简洁的总结( value code> check ,所以为了简洁起见,我将仅解释的价值



财产反映 def

属性反映了 value content attribute def





这句话很简洁。我跳过了一个重要的细节。现在这个规范很清楚,所以我只是引用 脏值标志 部分:


每个输入元素具有一个布尔值脏值标志。当创建元素时,脏值
标志必须初始设置为false,并且只要用户以
方式与控件进行交互来更改值,则
必须设置为true。 / p>

value content属性给出了输入
元素的默认值。当值内容属性被添加,设置或删除时,
如果控件的脏值标志为false,用户代理必须将
的值设置为值content属性的值
如果有一个,否则为空字符串,然后运行
当前值消毒算法,如果定义了一个。



Serializable property is defined as:

  • When you set a property of the element, it will be reflected in serialization queries such as getAttribute and you can see the changes in DOM Inspector
  • When you get the .innerHTML of the element's parentnode, the returned html string will contain all the serializable properties as their attribute counterparts

I have made a page that looks like it's reliably printing a table of all serializable properties of the input element in Chrome and Firefox: http://jsfiddle.net/tEVLp/16/. Custom properties are never serializable, so in firefox webkitSpeech etc are not serializable. Test in chrome for best results.

All booleans are true because serialization of a false property would be the absence of the attribute which is a false negative in the test.

So my question is, why are not properties such as .value and .checked serializable?

Technically, both are serializable. .value is just a string and the browser has no problems with serializing other boolean properties, such as .readOnly and .disabled.

My best guess is that since .defaultValue serializes to "value"-attribute and .defaultChecked serializes to "checked"-attribute, there would be a conflict and thus the .value and .checked cannot be serialized. In that case, why are the defaultX ones chosen for these and not the ones that reflect the more useful current .value and .checked states?

解决方案

The Specification for input elements defines the exact behaviour. Start reading from here (before that, the DOM interface, attributes and types are defined).

Concise summary (value is defined similarly to checked, so for brevity, I'm going to explain value only).

The "property" value reflects value def,
the "attribute" value reflects the value content attribute def.

  • The attribute defines the default value property (ref). This value is also reflected def by the defaultValue property (ref).
  • When the value attribute is set, then the value property changes (ref) *.

That was worded very concisely. I've skipped an important detail. The specification is very clear at this point, so I'll just quote the dirty value flag section:

Each input element has a boolean dirty value flag. The dirty value flag must be initially set to false when the element is created, and must be set to true whenever the user interacts with the control in a way that changes the value.

The value content attribute gives the default value of the input element. When the value content attribute is added, set, or removed, if the control's dirty value flag is false, the user agent must set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, and then run the current value sanitization algorithm, if one is defined.

这篇关于为什么一些技术上可序列化的输入属性是可序列化的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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