简体   繁体   中英

How to add a new html tag with Jsoup?

I'm having some trouble adding a new tag to my Document. For example I have:

Document doc = Jsoup.parse(htmlString);
Element table = doc.select("table").first();  

Now If I want to add a <LINK> tag with attributes (href,type,rel) to my table element, and then return the total as a string, How would I do this?

Use something like this:

Jsoup.parse(new URL(""), 0).getElementById("test").appendElement("h1").attr("id", "header").text("Welcome");

And all ".append*" methods.

It's been quite some time but I also searched through for it a while so I will share my answer. This is for Jsoup 1.13.1


Document doc = Jsoup.parse(htmlString);
Element table = doc.select("table").first();

table.appendElement("link").attr("href","example.com/file.css").attr("rel","stylesheet").attr("type","text/css");


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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