var script = document.createElement('script');script.src = "https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js";
document.head.appendChild(script);
var download_img = function (img_src, name) {
var image = new Image();
image.setAttribute("crossOrigin", "anonymous");
image.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
var url = canvas.toDataURL("image/png");
var a = document.createElement("a");
var event = new MouseEvent("click");
a.download = name || "photo";
a.href = url;
a.dispatchEvent(event);
};
image.src = img_src;
};
var tianmao_phone_main_pic = function () {
var num = 1;
$("ul.PicGallery--thumbnails--1cEhJzK li img").each(function (index) {
var index = num;// 当前图片的索引
var name = "img" + index + ".png";// 图片的名称
var url = $(this).attr("src");
var lastIndex = url.lastIndexOf("_110");
url = url.substring(0, lastIndex);
url = "https:" + url;
download_img(url, name);
num++;
});
};
tianmao_phone_main_pic();
标签:src,canvas,img,url,image,主图,天猫,var,电商
From: https://www.cnblogs.com/guxingy/p/16757068.html