Home JQuery下载页面所有图片
Post
Cancel

JQuery下载页面所有图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(function () {
    var imglist = $("body").find("img");
    for (var i = 0; i < imglist.length; i++) {
        downloadImage($(imglist[i]).attr("data-src"), i);
    }
});

function downloadImage(path, imgName) {
    var _OBJECT_URL;
    var request = new XMLHttpRequest();
    request.addEventListener('readystatechange', function (e) {
        if (request.readyState == 4) {
            _OBJECT_URL = URL.createObjectURL(request.response);
            var $a = $("<a></a>").attr("href", _OBJECT_URL).attr("download", imgName);
            $a[0].click();
        }
    });
    request.responseType = 'blob';
    request.open('get', path);
    request.send();
}
</script>
This post is licensed under CC BY 4.0 by the author.