我建立了一个使用Instagram API提取图像的Angular应用程序。如果用户以后删除图像,我将得到残破的图像(404图像)。我试图使用jQuery隐藏包含这些(损坏的)图像的div,但是它们仍然出现。
我已将以下jQuery放在“ index.html”文件中引用的“ custom.js”文件中:
$(document).ready(function() {
$("img").error(function() {
$(this).parent().hide();
});
});
我引用jQuery,然后在“ index.html”的开头引用“ custom.js”,如下所示:
<script src="libs/jquery/dist/jquery.min.js"></script>
<script src="js/custom.js"></script>
...这是我正在尝试将此jQuery应用于的html:
<a ng-href="{{image.link}}" target="_blank" title="{{image.text}}"><img ng-src="{{image.img}}" alt="" class="img-responsive" ng-style="homeColors" id="image"></a>
我最终使用以下Javascript隐藏了我的图像。第一个功能隐藏了我图像的祖父母,因此整个div都被隐藏了。第二个功能用Instagram的匿名用户图像替换丢失的用户个人资料图像:
function imgError(image){
image.parentNode.parentNode.style.display = 'none';
}
function anonImg(image){
image.src = 'https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg';
}
只需将以下HTML添加到相应的图像中:
onerror="imgError(this);"
onerror="anonImg(this);"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句