這些更改將在您製作文檔時應用於文檔。例如,我開始了與此:
String html = "<html>" +
"<body>" +
"<p class=\"class1\">p1</p>" +
"<p class=\"class2\">p2</p>" +
"</body>" +
"</html>";
Document doc = Jsoup.parse(html);
System.out.println(doc);
它輸出:
<html>
<head></head>
<body>
<p class="class1">class1</p>
<p class="class2">class2</p>
</body>
</html>
現在我做出一些改變到p元素:
Element p1 = doc.select("p.class1").first();
p1.attr("class", "classOne");
Element p2 = doc.select("p.class2").first();
p2.attr("id", "helloworld");
System.out.println(doc);
的輸出是不同的,以反映我對它的元素所做的更改:
<html>
<head></head>
<body>
<p class="classOne">p1</p>
<p class="class2" id="helloworld">p2</p>
</body>
</html>
謝謝是非常好的答案 – user3278450