Yar*_*rin 64 css css-selectors
默认输入类型是"text".我一直认为,input[type='text']
即使未在控件上显式声明类型,CSS声明目标也会影响这些输入.但是,我只是注意到我的默认类型文本输入没有获得样式.为什么会这样?我该如何解决这个问题呢?
input[type='text'] {
background: red;
}
Run Code Online (Sandbox Code Playgroud)
<input name='t1' type='text' /> /* Is Red */
<input name='t1' /> /* Is Not Red */
Run Code Online (Sandbox Code Playgroud)
Mr *_*ter 103
CSS仅使用DOM树中的数据,这与渲染器如何决定如何处理缺少属性的元素几乎没有关系.
所以要么让CSS反映HTML
input:not([type]), input[type="text"]
{
background:red;
}
Run Code Online (Sandbox Code Playgroud)
或者使HTML显式化.
<input name='t1' type='text'/> /* Is Not Red */
Run Code Online (Sandbox Code Playgroud)
如果它不这样做,你永远无法区分
element { ...properties... }
Run Code Online (Sandbox Code Playgroud)
和
element[attr] { ...properties... }
Run Code Online (Sandbox Code Playgroud)
因为所有属性都将始终在所有元素上定义.(例如,table
总是有一个border
属性,0
默认为.)
根据CSS规范,浏览器可能会也可能不会使用有关默认属性的信息; 大多是不要的.CSS 2.1规范中的相关子句是5.8.2 DTD中的默认属性值.在CSS 3选择器中,它是第6.3.4节,具有相同的名称.它建议:"选择器的设计应使其无论文档树中是否包含默认值,它们都能正常工作."
通常最好明确指定基本属性,type=text
而不是默认属性.原因是没有简单可靠的方法来引用input
具有默认type
属性的元素.
归档时间: |
|
查看次数: |
223569 次 |
最近记录: |