通过使用属性选择器或任何其他方式,查找类是否仅在 css 中具有边框。如果没有,则应用边框。意图是不退回到任何一个 jquery。这有可能实现吗?
编辑以包含@nag (OP) 的回复:
在 IE8 中,select
没有border
。所以我正在尝试像这样重置 css:
select, input[type="file"], textarea {
border: solid 1px #7F9DB9;
}
问题是由于特殊性,它覆盖了任何预先存在的样式。我尝试使用表达式过滤器,但在 IE8 中使用 DocType 它似乎不起作用。
最佳答案
CSS 没有if/else
语句的概念或实现,因此仅在 CSS 中这是不可能的。
但是,如果您为一个元素定义了 border
,然后又重新定义了 border
,则第二个语句将覆盖第一个(假设有一个同样具体的选择器),所以我不确定为什么你需要应用border
只有如果元素还没有border
定义:
div {
border: none; /* removes the border */
}
/* other stuff */
div {
border: 1px solid #f90; /* defines the border */
}
类似地:
div {
border: 5px solid #0f0; /* defines the border */
}
/* other stuff */
div {
border: 1px solid #f90; /* re-defines the border */
}
如果您可以定义您的用例,它可能会进一步帮助您。
编辑以解决问题中的更多信息:
In IE8 there is no border for select. So I'm trying to do a css reset like this:
select, input[type="file"], textarea {
border: solid 1px #7F9DB9;
}
The problem is it is overriding any preexisiting style because of specificity. I tried to use expression filter but with DocType in IE8 it does not seem to work.
如果问题是特异性,那么您唯一的选择是增加您要应用的选择器的特异性,最好使用id
,或者多个id
s,在您的选择器中(祖先元素的 id
很好),因为这是可用的最具体的选择器。
或者,您可以降低要覆盖的选择器的特异性。
值得注意的是,select
元素很难可靠地设置样式,因为它通常由底层操作系统呈现,而不是浏览器本身,以便在该操作系统中保持一致的外观。
关于css - 查找类是否在CSS中有边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834430/