小知识
最近做项目接触到一下小知识,我的记忆力向来不好,所以, 现在开始记录一下。
1.图片改成base64编码
function getBase64Image(img) {//图片编码
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
var dataURL = canvas.toDataURL("image/jpg");
return dataURL
}
function main() {
var img = document.getElementById('showimage');
var data = getBase64Image(img);
return data;
}
2.动态改变select的值
var all_options = document.getElementById("select").options;
for ( i = 0; i < all_options.length; i++) {
if (all_options[i].value.split("-")[0] == ret.content.split(",")[0])// 根据option标签的ID来进行判断 测试的代码这里是两个等号
{
all_options[i].selected = true;
}
}
3.获取select的显示内容text
var obj = document.getElementById("select");
var textStr= obj.options[obj.selectedIndex].text;
4.获取系统日期
function getDate() {
var myDate = new Date();
return myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
}
5.aui
添加或移除节点
$api.byId("guahao").classList.add("aui-active");
$api.byId("wode").classList.remove("aui-active");