使用JSoup设置HTML标记的属性

时间:2022-10-31 10:20:44

How to set attributes of tags of HTML using JSoup?

如何使用JSoup设置HTML标签的属性?

I want to set the attribute->"src" of tag->"img" in Java using Jsoup Library.

我想使用Jsoup库在Java中设置tag->“img”的属性->“src”。

Elements img_attributes = doc.select("img[src^=/im]");
for(Element img_attribute: img_attributes)
{

String s = img_attribute.attr("src");
System.out.println(s);
}

This code prints the src values. I want to change the src values.

此代码打印src值。我想改变src值。

3 个解决方案

#1


6  

You cen do this with attr() method in both ways: loop or directly on the Elements object:

你可以用attr()方法做这两种方法:循环或直接在元素对象上:

// 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");

In fact both solutions are the same.

事实上两个解是一样的。

#2


1  

check http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29 i think function

查看http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29我认为函数。

public Element attr(String attributeKey,
                    String attributeValue)

is useful for you

对你来说是有用的

#3


-1  

Please modify it and write it into your html file through doc.

请修改并通过doc将其写入您的html文件中。

#1


6  

You cen do this with attr() method in both ways: loop or directly on the Elements object:

你可以用attr()方法做这两种方法:循环或直接在元素对象上:

// 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");

In fact both solutions are the same.

事实上两个解是一样的。

#2


1  

check http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29 i think function

查看http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#attr%28java.lang.String,%20java.lang.String%29我认为函数。

public Element attr(String attributeKey,
                    String attributeValue)

is useful for you

对你来说是有用的

#3


-1  

Please modify it and write it into your html file through doc.

请修改并通过doc将其写入您的html文件中。