且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将整个HTML文档从字符串解析为jQuery

更新时间:2022-05-04 18:35:35

之所以不起作用,是因为jQuery期望DOM节点找到"title"标签.如您所述,您需要先解析html文本.

The reason why it does not work is because jQuery expects a DOM node to find the 'title' tags. As you noted, you need to parse the html text first.

此处

From here and here, the solution is to parse the string and append it into a temporal div (or other element):

var tempDom = $('<div></div>').append($.parseHTML(str));

然后,您可以操纵tempDom来查找元素.

Then, you can manipulate tempDom to find elements.

工作演示: http://codepen.io/anon/pen /wKwLMP