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;
$("div.content-container img").each(function (index) {
var index = num;// 当前图片的索引
var name = "img" + index + ".png";// 图片的名称
var url = $(this).attr("src");
console.log(url);
//var lastIndex = url.lastIndexOf("_");
//url = url.substring(0, lastIndex);
//url = "https:" + url;
download_img(url, name);
num++;
});
};
tianmao_phone_main_pic();
标签:src,canvas,img,小红书,image,url,详情,var,电商
From: https://www.cnblogs.com/guxingy/p/16756918.html