1.下载convas中的图片 👍
var convas = document.getElementById("canvas");
var dataURL = convas.toDataURL();
var link = document.createElement('a');
link.href=dataURL;
link.download='image.png';
document.body.appendChild(link);
link.click();
2.下载svg中的图片 👍
svg无法当成图片打开,可以使用工具转换成你想要的图片格式
var svg =document.getElementById("aaa")
var svgData=new XMLSerializer().serializeToString(svg);
var blob=new Blob([svgData],{type:"image/svg+xml;charset=utf-8"});
var url=URL.createObjectURL(blob);
var a=document.createElement("a");
a.href=url;
a.download="image.svg";
a.click();
写入cookie
//name=>key value=>cookie值 daysToExpire=>失效时间 传空则默认浏览器关闭就失效
function setCookie(name, value, daysToExpire) {
var expirationDate = '';
if (daysToExpire) {
var expirationTime = new Date();
expirationTime.setTime(expirationTime.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
expirationDate = "; expires=" + expirationTime.toUTCString();
}
document.cookie = name + "=" + value + expirationDate + "; path=/";
}
读取cookie
//读取cookie
function getCookie(name) {
var cookieName = name + "=";
var cookiesArray = document.cookie.split(';');
for (var i = 0; i < cookiesArray.length; i++) {
var cookie = cookiesArray[i].trim();
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return null;
}
转换成字符串
//举例:
//var boolValue = true;
//console.log(String(boolValue) === "true");
function boolToString(val){
return String(val);
}
获取checkbox是否选中
//ele => $(".chkbox");
//获取checkbox选中属性
function getChkboxIsSelect(ele){
ele.prop("checked");// true / false
}
使用父级页面的function时,判断funciton是否存在
//父级方法存在,则调用
function fatherfunctionIsExist(){
//funcName 为你要使用的父级function
if (window.parent && typeof window.parent.funcName === 'function') {
window.parent.funcName();
}
}
获取url中的参数
//返回值是object {a:"我是a",b:"我是b"}
function getUrlParameters() {
var urlParams = {};
var match,
regex = /([^&=]+)=([^&]*)/g;
var searchString = window.location.search.substring(1);
while ((match = regex.exec(searchString))) {
var key = decodeURIComponent(match[1]);
var value = decodeURIComponent(match[2]);
urlParams[key] = value;
}
return urlParams;
}
评论区