Remove Bootstrap's Carousel item if image not found

て烟熏妆下的殇ゞ 提交于 2019-12-12 00:07:10

问题


when the page loads, if the image returns a 404 not found error I would like to remove the wrapping div.item so the carousel continues as if.. how can I achieve this?

this was my logic.. but no luck

$(window).load(function() {
    $(".item img").error(function () { 
        $(this).parent('div.item').remove();
    });
});

bootstrap carousel markup


回答1:


On the image tag you can add the onerror event

<img src="404imagegoeshere" onerror="functionToRemoveWrapping(this)">

Then on your function you can do something like this

    <script>
         function functionToRemoveWrapping(image) {
           setTimeout(function(){
             $(image).parent().remove;
           }, 2000);    
         }
    </script>

The 2000 value is in miliseconds.



来源:https://stackoverflow.com/questions/37728140/remove-bootstraps-carousel-item-if-image-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!