简体   繁体   English

java swing div html的属性样式

[英]java swing get attribute style of div html

I want to get url img "assets/img/slider1.jpg". 我想获取网址img“ assets / img / slider1.jpg”。 file index.hrml : 文件index.hrml:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <div class="carousel-content" style="background-image:url(assets/img/slider1.jpg)"></div>                                           
    </body>
</html>

I use the code below it to null. 我使用下面的代码为null。 Result Img: null. 结果图:空。

Please help me. 请帮我。

public class getStyleHTML {

    public static void main(String[] args) throws BadLocationException, IOException {
        // TODO Auto-generated method stub
        try {
            URL url = new URL("http://localhost/test/test/index.html");
            HTMLEditorKit kit = new HTMLEditorKit(); 
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
            Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream());
            kit.read(HTMLReader, doc, 0);
            ElementIterator it = new ElementIterator(doc);
            Element elem = null;
            while ( (elem = it.next()) != null ){ 
                if( elem.getName().equals("div")){ 
                   String div = (String) elem.getAttributes().getAttribute(HTML.Attribute.STYLE);
                   System.out.println("Img: "+div);
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}

Thank you! 谢谢!

you can use css command to get background-image . 您可以使用css命令获取background-image Then, you can get rid of url syntax as shown below: 然后,您可以摆脱url语法,如下所示:

var string =$("div").css("background-image");
string = string.replace('url(','').replace(')','');

The string contains the url content. string包含url内容。 Look at the working example here . 这里查看工作示例。 You need to use jQuery by the way. 您需要顺便使用jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM