How can i clone an image in javascript

后端 未结 2 824
旧巷少年郎
旧巷少年郎 2021-02-13 13:27


I\'m trying to clone an image in javascript, bud without loading a new one.
Normally new browsers will load an image once and there are several ways to use that image

2条回答
  •  北海茫月
    2021-02-13 14:07

    Afaik the default browser behavior is to cache images. So something like this should work the way you want:

     var sourceImage = document.createElement('img'),
         imgContainer = document.getElementById("content");
     sourceImage.src = "[some img url]";
     imgContainer.appendChild(sourceImage);
    
     function cloneImg(){
         imgContainer.appendChild(sourceImage.cloneNode(true));
     }
    

    It's all pretty sop, so it should run in IE6 too (I don't have it, so can't test that). See it in action

    Furthermore, you may want to check the cache setting of your IE6 browser. I remember from the not so good old days with IE<8 that I sometimes reverted to setting the cache to refresh "every time you load the page" (or someting like that).

提交回复
热议问题