CSS 解析器/抽象器?如何将样式表转换为对象
是否有标准或可靠的方法可供 JavaScript 框架(例如 jquery)将样式表解析为对象?
我想知道的两个原因:
我看到了几个问题,其中有人想知道如何获取由样式表为选择器设置的样式属性,而不是选择器最终继承的样式属性。
如果 Sizzle 做了它应该做的事情,这可能是确保样式表正确呈现的解决方案跨浏览器。基本上让 jquery 解析样式表并手动设置所有属性(可能基于浏览器或已知未实现的选择器。)这样设计者/开发人员只需编写向前兼容的 CSS3 样式表,并让 jquery/sizzle 完成浏览器赢得的工作't。
我看到的唯一缺点是这仍然不会实现 CSS3 属性,但它是一个开始。
Is there a standard or reliable method already out there for a javascript framework such as jquery to parse a stylesheet into an object?
Two reasons for why I'm wondering:
I have seen a couple of questions where someone wanted to know how to get the style attribute that was set by the stylesheet for a selector, not what the selector eventually inherited.
If Sizzle does what it is supposed to, this could be a solution for making sure a stylesheet got rendered correctly cross-browser. Basically have jquery parse the stylesheet and set all of the attributes manually (maybe based on browser or known unimplemented selectors.) that way the designers/developers just write a CSS3 stylesheet that is forward compatible and have jquery/sizzle do the work the browser won't.
The only downside I see is that this still won't implement CSS3 attributes, but it's a start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题。 Daniel Wachsstock 的站点上有一个 jquery 解析器。 http://bililite.com/blog/2009/01/16 /jquery-css-parser/
不幸的是,它可能不是您正在搜索的内容......但值得一试。以下描述摘自他的网站:
在 jQuery 中,您调用
$(selector).parsecss(callback)
,它会扫描 $(selector) 或其后代中的所有元素,解析每个元素并传递一个对象(详情如下)到回调函数。
例如创建一个 CSS 文件:
你会得到
Interesting question. There is a jquery parser over at Daniel Wachsstock's site. http://bililite.com/blog/2009/01/16/jquery-css-parser/
Unfortunately it may not be what you are searching for...but it's worth a try. The following description is taken from his site:
In jQuery you call
$(selector).parsecss(callback)
which scans all and elements in $(selector) or its descendents, parses each one and passes an object (details below) to the callback function.
For instance create a CSS file:
and you get
在客户端,样式表已经是一个对象;当页面加载时它被解析成树。
假设您有一个以
global.css
开头的 HTML 页面,其中包含以下行:Then,为了找到
#page
background-color 的设置内容code>,您需要编写document.styleSheets[0].cssRules[0].style.backgroundColor
,它将返回#fff
(或rgb (255, 255, 255)
在某些浏览器上,我认为)。其他有用的内容假设上述工作表:
如果您有一个更复杂的选择器,例如
#main div.header a, #main div.header a:visited
,那么选择器文本属性将返回整个内容,不仅仅是第一点。您的具体问题是“如何找出给定选择器的样式表中设置的内容”。这是一种近似方法:
问题是,您以这种方式访问的 CSS 树已经被浏览器解析了(因此是上面的“近似值”)。如果您使用的是 Firefox,则不会看到任何
-webkit
样式,因为 mozilla 会删除它们。各种浏览器也往往有自己的显示颜色的方式(如上所述;如果您实际的 .css 文件将给定颜色设置为#fff
,并且您在解析后在 JavaScript 中检查它,您可能会返回#fff
、#ffffff
或rgb(255, 255, 255)
)。上面的内容将告诉您浏览器将该 CSS 表视为什么;如果您想知道初始 .css 文件中包含哪些特定的 ascii 字符,唯一可靠的方法是查看文件本身,据我所知。
此处提供了样式表对象的参考。
On the client-side, a stylesheet is already an object; it gets parsed into a tree when the page loads.
Lets say you have an HTML page starting with
and
global.css
contains the linesThen, in order to find what's set for
background-color
of#page
, you'd need to writedocument.styleSheets[0].cssRules[0].style.backgroundColor
, which would return#fff
(orrgb(255, 255, 255)
on some browsers, I think).Other useful stuff assuming the above sheets:
If you had a more complex selector, like
#main div.header a, #main div.header a:visited
, then the selector text property returns the entire thing, not just the first bit.Your specific question is "How can I find out what is set in the stylesheet for a given selector". Here's one way to approximate it:
The thing is that the CSS tree you have access to in this way has already been parsed by the browser (hence the 'approximate' above). If you're in Firefox, you won't see any
-webkit
styles because mozilla just drops them. Various browsers also tend to have their own way of displaying colors (as above; if your actual .css file has a given color set to#fff
, and you check it in JavaScript after it's parsed, you might get back#fff
,#ffffff
orrgb(255, 255, 255)
).The above will tell you what your browser sees that CSS sheet as; if you want to know what specific ascii characters were contained in the initial .css file, the only reliable way is to look at the file itself, AFAIK.
A reference for the stylesheet object is available here.