In the front-end page design of the site, to consider the existence of IMG image resources, if the IMG src Image resource does not exist or display, you need to display the default picture. How do we do that?
One, the error event of monitoring document
Document.addeventlistener ("Error", Function (e) {varElem =E.target; if(elem.tagName.toLowerCase () = = ='img') { varNOTFOUNDIMGSRT = $ (elem). attr ("notfoundsrc"); if(NOTFOUNDIMGSRT) {$.Get(NOTFOUNDIMGSRT,NULL, function (response, status, XHR) {if(Xhr.status = = = $) Elem.src=notfoundimgsrt; }); } }}, true);
The IMG onerror event is not bubbling, so we have to manually write the code and listen for the error event of the document. If the target of the current error event is an IMG tag, locate the Notfoundsrc property of the IMG tag and use AJAX to determine whether the default picture exists for the current site, and if the default picture exists, assign it the SRC attribute of the IMG.
Second, set the Notfoundsrc property of the IMG tag
<img src="xxxxx" alt=" notfoundsrc="images/ Imgnotfound.png"/>
As can be seen by code, XXXXX is not a path to a real picture resource, so:
1, the IMG tag will trigger its error event;
2, because we have customized a document error monitoring event, then the document will capture the error event that IMG sent;
3. Execute the logic of the error event.
JS settings img Tag src Resource cannot find an alternative picture (via IMG property settings)