我从其他页面加载 HTML 以从该页面提取和显示数据:
$.get('http://example.org/205.html', function (html) {
console.log( $(html).find('#c1034') );
});
这确实有效,但由于 $(html)
,我的浏览器尝试加载在 205.html 中链接的图像。这些图像在我的域中不存在,所以我收到很多 404 错误。
有没有一种方法可以像 $(html)
一样解析页面,但无需将整个页面加载到我的浏览器中?
最佳答案
实际上,如果您查看 jQuery documentation它说您可以将“所有者文档”作为第二个参数传递给 $
。
那么我们可以做的是create a virtual document这样浏览器就不会自动加载提供的 HTML 中存在的图像:
var ownerDocument = document.implementation.createHTMLDocument('virtual');
$(html, ownerDocument).find('.some-selector');
关于javascript - jQuery解析HTML而不加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113910/