點擊下載書簽的實現
阿新 • • 發佈:2017-09-09
ati lan for 地址 per nload event eating load
模擬下載的實現
對於鏈接可以添加download屬性來實現點擊下載,當給download屬性設置值時會作為文件名下載。
<a id="download" href="/pk1.png" download="filename">下載</a>
也可以通過選擇後,模擬下載即
function downloadImg(imgsrc,fileName){ var aLink=document.createElement(‘a‘); aLink.href=imgsrc; aLink.download=(fileName+‘‘)||‘‘; var evt = document.createEvent("MouseEvent"); evt.initEvent("click", false, false); aLink.dispatchEvent(evt); }
其中imgsrc問圖片的地址,filename為要下載為對應的文件名。
詳見
書簽功能的實現
以chrome 為例,把URL的內容改為
javascript:var imgs=document.querySelectorAll(‘img‘); for(var i=0;i<imgs.length;i++){ if(imgs[i].width>600){ downloadImg(imgs[i].src); } } function downloadImg(imgsrc,fileName){ var aLink=document.createElement(‘a‘); aLink.href=imgsrc; aLink.download=(fileName+‘‘)||‘‘; var evt = document.createEvent("MouseEvent"); evt.initEvent("click", false, false); aLink.dispatchEvent(evt); };
就能夠點擊後自動下載已經加載的寬度大於600px的圖片。
點擊下載書簽的實現