backgroundImageで画像ファイルが存在しなかった場合にimgタグ同様onerrorを発生させるにはどうしたらいいでしょうか?
<script> window.onload=function(){ div=document.createElement("div"); div.style.width="100px"; div.style.height="100px"; div.style.backgroundImage="url(https://teratail.com/error.jpg)"; div.style.backgroundRepeat="no-repeat"; div.style.backgroundSize="contain"; div.style.backgroundPosition="center"; div.addEventListener('error', function() { console.log("error"); }); document.body.appendChild(div); } </script>
```ここに言語を入力 <script> window.onload=function(){ img=document.createElement("img"); img.src="https://teratail.com/error.jpg"; img.style.width="100px"; img.style.height="100px"; img.addEventListener('error', function() { console.log("error"); }); document.body.appendChild(img); } </script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/08/19 01:34
2021/08/19 01:40
退会済みユーザー
2021/08/19 01:42 編集
2021/08/19 01:51 編集