如何使用 Cobra/Lobo HTML 渲染器访问 DOM 节点的 CSS 属性
我正在尝试弄清楚如何使用 Cobra/Lobo 工具包访问 DOM 节点(在本例中为 节点)的 CSS 属性。我现在所拥有的是:
UserAgentContext uacontext = new SimpleUserAgentContext();
DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);
URL url = new URL(TEST_URI);
InputStream in = url.openConnection().getInputStream();
Reader reader = new InputStreamReader(in, "ISO-8859-1");
InputSourceImpl inputSource = new InputSourceImpl(reader, TEST_URI);
HTMLDocumentImpl d = (HTMLDocumentImpl) builder.parse(inputSource);
HTMLCollection images = d.getImages();
for (int i = 0; i < images.getLength(); i++) {
HTMLElementImpl n = (HTMLElementImpl) images.item(i);
AbstractCSS2Properties curr = n.getCurrentStyle();
System.out.println("Image " + i + ": " + curr.getPropertyValue("background-color"));
}
现在这似乎只给我直接设置的样式 - 而不是继承或计算的样式。我怎样才能得到这些呢?
谢谢
I am trying figure out how to access CSS properties for a DOM node (in this example <img> nodes) using the Cobra/Lobo toolkit. What I have at the moment is:
UserAgentContext uacontext = new SimpleUserAgentContext();
DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);
URL url = new URL(TEST_URI);
InputStream in = url.openConnection().getInputStream();
Reader reader = new InputStreamReader(in, "ISO-8859-1");
InputSourceImpl inputSource = new InputSourceImpl(reader, TEST_URI);
HTMLDocumentImpl d = (HTMLDocumentImpl) builder.parse(inputSource);
HTMLCollection images = d.getImages();
for (int i = 0; i < images.getLength(); i++) {
HTMLElementImpl n = (HTMLElementImpl) images.item(i);
AbstractCSS2Properties curr = n.getCurrentStyle();
System.out.println("Image " + i + ": " + curr.getPropertyValue("background-color"));
}
Now this only seems to give me directly set styles - not inherited or computed styles. How can I get these as well?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望您需要使用 HTMLElementImpl 类。
I expect you need to use the
getComputedStyle
method of Class HTMLElementImpl.