var 소스는 존재하는지 확인하고 그렇지 않은 경우 서버에있는 로컬 이미지로 바꿀 수 있습니다 (예 : "/images/default.jpg")?바닐라 JS 원격 이미지가 있는지 확인하십시오. 그렇지 않으면 fallback을 사용 하시겠습니까?
(function() {
var youtube = document.querySelectorAll(".youtube");
for (var i = 0; i < youtube.length; i++)
{
var source = "https://img.youtube.com/vi/"+ youtube[i].dataset.embed +"/sddefault.jpg";
var image = new Image();
image.src = source;
image.addEventListener("load", function() {
youtube[ i ].appendChild(image);
}(i));
youtube[i].addEventListener("click", function() {
var iframe = document.createElement("iframe");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "");
iframe.setAttribute("src", "https://www.youtube.com/embed/"+ this.dataset.embed +"?rel=0&showinfo=0&autoplay=1");
this.innerHTML = "";
this.appendChild(iframe);
});
};
})();
과 같은
this.src
속성을 대체하십시오. 이미지 객체에 onerror 이벤트를 사용할 수 있습니다. – KVNSTOBJEKT