[英]Is there any difference between “value” attribute to be empty and no value attribute at all in html?
Is there any difference between the following? 以下内容之间有什么区别吗?
<input value/>
<input value=""/>
<input />
Yes there are differences, but only between two firsts and last one : from specs 是的,有区别,但只有两个首尾之间: 从规格
Note that empty attribute syntax is exactly equivalent to specifying the empty string as the value for the attribute.
请注意,空属性语法与将空字符串指定为属性值完全等效。
JS wise: two first will make getAttribute
return an empty string, while last one will return null
. JS明智的选择:前两个将使
getAttribute
返回一个空字符串,而后一个将返回null
。
For all 3, value
property will be an empty string. 对于所有3,
value
属性将是一个空字符串。
CSS wise, input[value]
will match only the 2 firsts. 在CSS方面,
input[value]
仅匹配2个first。
var inp = document.querySelectorAll('input'), attr; for (var i = 0; i < inp.length; i++) { attr = inp[i].getAttribute('value') console.log(i, 'attr:', '[' + typeof attr + '] ' + attr, 'val:', '[' + typeof inp[i].value + '] ', inp[i].value); }
input[value] { background: red; }
<input value/> <input value="" /> <input>
But note that this is only for <input>
tag. 但是请注意,这仅用于
<input>
标记。
Other elements have other behaviours (eg MediaElement and the controls
attribute) 其他元素具有其他行为(例如MediaElement和
controls
属性)
var vid = document.querySelectorAll('video'), attr; for (var i = 0; i < vid.length; i++) { attr = vid[i].getAttribute('controls') console.log(i, 'attr:', '[' + typeof attr + '] ' + attr, 'val:', '[' + typeof vid[i].controls + '] ', vid[i].controls); }
<video controls></video> <video controls=""></video> <video controls="true"></video> <video controls="false"></video>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.