设置属性值

Problem

You have a parsed document that you would like to update attribute values on, before saving it out to disk, or sending it on as a HTTP response.html

Solution

Use the attribute setter methods Element.attr(String key, String value), andElements.attr(String key, String value).java

If you need to modify the class attribute of an element, use the Element.addClass(String className) and Element.removeClass(String className) methods.node

The Elements collection has bulk attribue and class methods. For example, to add arel="nofollow" attribute to every a element inside a div:api

doc.select("div.comments a").attr("rel", "nofollow");

Description

Like the other methods in Element, the attr methods return the current Element (or Elementswhen working on a collection from a select). This allows convenient method chaining:ide

doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");