现在我用它来创建一个元素
<!-- language: lang-java -->
Document doc = Jsoup.parseBodyFragment("<ol></ol>");
Elements ols=doc.getElementsByTag("ol");
Element ol=ols.get(0);
Run Code Online (Sandbox Code Playgroud)
但这太复杂了,因为我在程序中创建了很多dom,如果每次使用三行来创建一个元素,那就不方便了.
我可以在不使用文档和元素的情况下创建元素吗?
像这样:
<!-- language: lang-js -->
var ol=$('<ol></ol>');
Run Code Online (Sandbox Code Playgroud)
尝试使用Elements构造函数:
Element el = new Element(Tag.valueOf("ol"), "");
Run Code Online (Sandbox Code Playgroud)
小智 5
从版本 1.10.2 开始,您可以Element像这样使用 的构造函数:
Element el = new Element("ol");
Run Code Online (Sandbox Code Playgroud)
构造函数及说明:
public Element(String tag)
Create a new, standalone element.
Parameters:
tag - tag name