指定された画像が存在しないときに別の画像に置換する

Ads

Result

jQuery

$(document).ready(function() {
            //画像にclassを付加
            $('img').addClass('miss');
            //画像が見つからないときにエラーイベント発生
            $('img.miss').error(function() {
                //置換処理
                $(this).attr({
                    src: 'http://example.com/no-images.jpg',
                    alt: 'none image',
                    style:'border: 1px solid #eee;width:150px;height:150px;margin:25px;'
                });
            });
        });

html

<img src="nothing.jpg" />

via

Use Custom Missing Image Graphics Using jQuery

タイトルとURLをコピーしました