代码之家  ›  专栏  ›  技术社区  ›  Richard H

如何使用cobra/lobo html呈现器访问dom节点的css属性

  •  0
  • Richard H  · 技术社区  · 15 年前

    我正在尝试如何使用cobra/lobo工具包访问dom节点(在本例中为<img>节点)的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"));
        }
    

    现在,这似乎只给我直接设置样式-而不是继承或计算样式。我怎样才能得到这些呢?

    谢谢

    1 回复  |  直到 15 年前
        1
  •  0
  •   ChrisW    15 年前

    我想你需要用 getComputedStyle 方法 Class HTMLElementImpl .