避免jQuery(pre)加载图片 [英] Avoid jQuery (pre)loading images

查看:42
本文介绍了避免jQuery(pre)加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一串HTML用作jQuery输入文档。

I have a string of HTML that I use as jQuery input document.

// the variable html contains the HTML code

jQuery( html ).find( 'p' ).each( function( index, element ) {
    // do stuff
});

尽管如此,我仍然会收到图片不存在的错误(在我的变量中 HTML )。 HTML中的图像具有相对URL,它与我主机上的图像不对应,所以当然,它们无法找到,所以我在控制台中出现404错误。

Though, I keep getting errors that the images doesn't exist (in my variable html). The images in the HTML have relative URL's which doesn't correspond to images on my host, so naturally, they can't be found so I get 404 errors in my console.

是否有jQuery方法可以避免jQuery加载图像?如果没有,我将不得不找到所有图像并使用非jQuery替换src,这有点令人伤心,因为那正是jQuery派上用场:p

Is there a jQuery-way to avoid jQuery loading the images? If not, I'll have to find all images and replace the src using non-jQuery which is a bit sad because that's exactly where jQuery comes in handy :p

编辑:

I我将逐步解释。

我有一个变量 html ,其中包含来自其他网站的一些HTML代码。我将该变量放在jQuery构造函数中,因为我想对该HTML执行操作。

I have a variable, html, that contains some HTML code from another site. I put that variable in the jQuery constructor, because I want to perform actions on that HTML.

jQuery(html);

此时我收到404错误,因为HTML源代码中的图像是相对的;它们与我在主机上的图像不对应。

At this point I get 404 errors because the images in the HTML source are relative; they don't correspond to the images I have on my host.


  • 我无法使用jQuery删除src标签,因为我仍然会收到错误

  • 编写一些javascript来修改src值是很愚蠢的;这就是为什么我使用jQuery

所以,再次,所有我要问的是,是否有设置或其他什么可以避免jQuery来自加载提供的源代码中的图像。

谢谢。

推荐答案

要停止加载图片,您需要在创建 jQuery(html)对象之前修改html 。您也可以使用jquery。

To stop images loading you need to modify html before creating jQuery( html ) object. You can use jquery for that also.

var newhtml = html.replace('src=','nosrc=');
jQuery( newhtml ).find( 'p' ).each( function( index, element ) {
    // do stuff
});

这篇关于避免jQuery(pre)加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆