as this question is the first result at search engines
There is anare a problem with the selected -and right by the way- solution, is that if you want to add style that will apply to images like ( borders for example ) .
for example :
img {
color:#fff;
border: 1px solid black;
padding: 5px;
background-color: #ccc;
}
<img src="http://badsrc.com/blah" alt="BLAH BLAH BLAH" /> <hr />
<img src="https://cdn4.iconfinder.com/data/icons/miu-square-flat-social/60/stackoverflow-square-social-media-128.png" alt="BLAH BLAH BLAH" />
as you can see, all of images will apply the same style
there is another approach to easily work around such an issue, using onerror
and injecting some special class to deal with the interrupted images :
.invalidImageSrc {
color:#fff;
border: 1px solid black;
padding: 5px;
background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<img onerror="$(this).addClass('invalidImageSrc')" src="http://badsrc.com/blah" alt="BLAH BLAH BLAH" /> <hr />
<img onerror="$(this).addClass('invalidImageSrc')" src="https://cdn4.iconfinder.com/data/icons/miu-square-flat-social/60/stackoverflow-square-social-media-128.png" alt="BLAH BLAH BLAH" />