Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery在不加载图像的情况下解析HTML_Javascript_Jquery_Ajax_Image - Fatal编程技术网

Javascript jQuery在不加载图像的情况下解析HTML

Javascript jQuery在不加载图像的情况下解析HTML,javascript,jquery,ajax,image,Javascript,Jquery,Ajax,Image,我从其他页面加载HTML以从该页面提取和显示数据: $.get('http://example.org/205.html', function (html) { console.log( $(html).find('#c1034') ); }); 这确实有效,但由于$(html)我的浏览器尝试加载205.html中链接的图像。这些图像在我的域中不存在,所以我得到了很多404错误 有没有办法像解析$(html)那样解析页面,但不将整个页面加载到我的浏览器中?使用regex并删除所有标记

我从其他页面加载HTML以从该页面提取和显示数据:

$.get('http://example.org/205.html', function (html) {
    console.log( $(html).find('#c1034') );
});
这确实有效,但由于
$(html)
我的浏览器尝试加载205.html中链接的图像。这些图像在我的域中不存在,所以我得到了很多404错误


有没有办法像解析
$(html)
那样解析页面,但不将整个页面加载到我的浏览器中?

使用regex并删除所有
标记

 html = html.replace(/<img[^>]*>/g,"");
html=html.replace(/]*>/g,”);

您可以使用jQuerys
remove()
方法来选择图像元素

console.log( $(html).find('img').remove().end().find('#c1034') );
或者从HTML字符串中删除。差不多

console.log( $(html.replace(/<img[^>]*>/g,"")) );

使用以下方法解析html将自动加载图像

var wrapper = document.createElement('div'),
    html = '.....';
wrapper.innerHTML = html;

如果使用
DomParser
解析html,图像将不会自动加载。有关详细信息,请参见。

以下正则表达式替换了所有出现的
,包括ajax加载返回的数据字符串中的
背景
样式
属性

html = html.replace(/(<(\b(img|style|script|head|link)\b)(([^>]*\/>)|([^\7]*(<\/\2[^>]*>)))|(<\bimg\b)[^>]*>|(\b(background|style)\b=\s*"[^"]*"))/g,"");
html=html.replace(/(]*\/>)|([^\7]*(]*>))|(]*>|(\b(背景风格)\b=\s*“[^”]*”)/g“”;
测试正则表达式:


我希望有一种更好的解决方法(而不是使用regex replace)。

很抱歉重新提出了一个老问题,但这是搜索如何停止加载外部资产的解析html时的第一个结果

我接受了Nik Ahmad Zainalddin的回答,但是其中有一个缺点,即
标记之间的任何元素都会被删除

<script>
</script>
Inert text
<script>
</script>
此外,我还添加了删除
iframe
s的功能


希望这对某人有所帮助。

您可以使用以下正则表达式删除所有src属性,而不是完全删除所有img元素:

html = html.replace(/src="[^"]*"/ig, "");

实际上,如果您查看,它会说您可以将“owner document”作为第二个参数传递给
$

因此,我们可以做的是,浏览器不会自动加载所提供HTML中的图像:

var ownerDocument = document.implementation.createHTMLDocument('virtual');
$(html, ownerDocument).find('.some-selector');

这对我来说很有效。注意,它不适用于样式背景图像。因此,我猜你需要一个。谢谢!@PiTheNumber&Bhuvan:FWIW,这个正则表达式是很容易绕过的:我想它可以在重复应用中工作,但我不想打赌我的网站上没有人能够想出解决它的方法。正则表达式是fundam本质上不适合重要的HTML解析。@T.J.Crowder我知道它不能保存,但在我的情况下,我可以信任其他域的HTML代码。Regex对大多数事情都不好,我强烈建议尽可能避免它。我很高兴看到另一个解决方案,但完整的HTML解析器对此来说太大了。第一个不起作用。
$(html)
已经加载了页面。第二个成功了。谢谢!这会破坏html,因为对于
元素,src属性是必需的。请看,这可能是真的,但对于在css选择器中使用img标记或需要某个图像属性的数据的任何人来说,这是一个很好的替代解决方案。我没有测试过这一点,但它看起来很好我喜欢这个问题的最佳解决方案。如果它不起作用,让我知道。你仍然可以使用下面的字符串替换,但我一直认为它是一个。谢谢,这是我需要的
html = html.replace(/src="[^"]*"/ig, "");
var ownerDocument = document.implementation.createHTMLDocument('virtual');
$(html, ownerDocument).find('.some-selector');