如何使用JSoup设置HTML的标签属性?
我想用Jsoup库在Java中设置属性->“src”of tag->"img“。
Elements img_attributes = doc.select("img[src^=/im]");
for(Element img_attribute: img_attributes)
{
String s = img_attribute.attr("src");
System.out.println(s);
}这段代码打印src的值。我想更改src的值。
发布于 2013-01-24 22:19:48
您可以通过两种方式使用attr()方法完成此操作:循环或直接在Elements对象上执行:
// In a loop
for( Element img : doc.select("img[src]") )
{
img.attr("src", "your-source-here"); // set attribute 'src' to 'your-source-here'
}
// Or directly on the 'Elements'
doc.select("img[src]").attr("src", "your-value-here");事实上,这两种解决方案都是相同的。
发布于 2013-01-24 13:37:32
检查http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29 I think函数
public Element attr(String attributeKey,
String attributeValue)对你很有用
发布于 2013-01-24 13:35:49
请修改并通过文档将其写入您的html文件。
https://stackoverflow.com/questions/14494589
复制相似问题