为DOM元素';s属性?

为DOM元素';s属性?,dom,attributes,Dom,Attributes,我只是偶然发现了这样的东西 function(element) { ... var attributes = element.attributes; for (var index = 0, length = attributes.length; index < length; index++) { var attribute = attributes[index]; // what is ".specified"? if (attribute.spec

我只是偶然发现了这样的东西

function(element) {
  ...
  var attributes = element.attributes;
  for (var index = 0, length = attributes.length; index < length; index++) {
    var attribute = attributes[index];

    // what is ".specified"?
    if (attribute.specified) {
      ...
    }
  }
}
函数(元素){
...
var属性=element.attributes;
对于(var index=0,length=attributes.length;index
我正在查看一个DOM元素的W3C规范,元素接口,我没有看到指定的


指定的属性是什么意思?它代表什么?规范中在哪里定义了它?

。该属性为
src
,其值为
kittens.jpg
。DOM元素是泛型定义。实际属性由实际使用的语言指定,例如XML、HTML等

specified=该属性具有指定的显式值。e、 指定了src属性,因为它的值为kittens.jpg,但是

<input type="checkbox" checked />


选中的属性存在,但未指定。

不同的浏览器具有不同的DOM实现,请注意,该实现负责该属性,而不是用户。但是,用户可以更改该属性的默认值

至于Chrome54.0.2840.71,它使每个属性都为true,要么在DOM规范中,要么没有,要么有值。 例如,指定的属性
\u test
在Chrome(版本54)中始终为真


文件
var div=document.querySelector(“div”);
var属性=div.attributes;
var attr_test=属性[0];
attr_test.specified=false;//“指定的”不可写,默认设置失败。
控制台日志(指定属性测试);
所以我同意“它正在被贬低”

DOMLevel3中的“attribute.specified”与DOMLevel2规范中的不同

1.同一点

如果该属性在实例文档中显式给定了值,则为True,否则为false。如果应用程序更改了此属性节点的值(即使它最终具有与默认值相同的值),则将其设置为true

2.差异
“DOM-Level2”有更复杂的判断条件来决定“attribute.specified”是否为真。
“DOM-Level3”说

该实现可以类似地处理来自其他模式的具有默认值的属性,但应用程序应该使用Document.normalizeDocument()来确保此信息是最新的



vs

我刚刚发现属性节点的属性
指定的信息仅对IE 6-7有意义,因为在IE 6-7中
属性
返回特定元素节点支持的所有属性的集合。然后,您可以使用
specified
确定集合中的属性是否附加到元素节点。如果元素节点没有指定/定义此属性,则返回false。在现代浏览器中,
attributes
返回附加到元素的属性集合事实上,这意味着在现代浏览器中,从集合中指定的每个
属性都将返回
true
。对于现代浏览器,
element.hasaAttribute(attribute)
的工作方式与
element.attributes[attribute].specified
对IE 6-7的工作方式相同。

您考虑过这个吗?是的,这就是我要说的我不知道它在规范中的定义,但是我发现它们的定义非常充分:如果指定了属性,指定的属性将返回true。如果属性已创建但尚未附加到元素,则也返回true。否则返回false。我相信它正在被贬低。这为你澄清了一切吗?@Paweł。没有
specified
仅表示该属性存在于原始HTML中,或者是由脚本使用
setAttribute()
设置的<已指定示例中的代码>已选中
。示例:。嘿,蒂姆。。。你介意用更多的细节来创造一个你自己的答案吗?
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div __test></div>
    <script>
        var div=document.querySelector("div");
        var attributes = div.attributes;
        var attr_test=attributes[0];
        attr_test.specified=false;// "specified" is not writable and Setting it silently fails.
        console.log(attr_test.specified);
    </script>
</body>
</html>