一:js对时间格式的转化
超级有用的函数,本人亲测
//设定时间格式化函数,使用new Date().format("yyyy-MM-dd HH:mm:ss");
Date.prototype.format = function (format) {
var args = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"H+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
};
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var i in args) {
var n = args[i];
if (new RegExp("(" + i + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
}
return format;
};
1.js获取当前时间
function getDatetime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hh = now.getHours();
var mm = now.getMinutes();
var ss = now.getSeconds();
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm + ":";
if (ss < 10) clock += '0';
clock += ss;
return clock;
}
二:几种点击事件的监听方式
1.使用document元素的addEventListener方法
var search = document.getElementById("icoSearch")
search.addEventListener("click",function () {
console.log("监听成功")
})
2.使用jQuery的监听方式
第一种写法
$("search").click(function () {
console.log("监听成功")
})
第二种写法
$("search").on("click",function () {
console.log("监听成功")
})
二:form表单传数据,以及上传任意格式的文件
1.上传纯文体和正常数据传输
默认的编码方式。但是在用文本的传输和MP3等大型文件的时候,使用这种编码就显得 效率低下。
<form enctype="application/x-www-form-urlencoded" action="/login">
<input type="text" name="username">
<input type="submit" value="提交">
</form>
纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码
<form enctype="text/plain" action="/login">
<input type="text" name="username">
<input type="submit" value="提交">
</form>
2.上传图片、视频、文件夹等
<form enctype="multipart/form-data" action="/login">
<input type="file" name="username">
<input type="submit" value="提交">
</form>
由于正常的input标签上传会出现类似于下面的后缀
本人提供一种方法仅供参考
<script>
window.onload = (event) = > {
main();
}
function main(){
const inputButton = document.querySelector("input[type='checkbox']")
const inputFile = document.querySelector("input[type='file']")
inputButton.onclick = (event) => {
// sleep(5000)
inputFile.click();
}
inputFile.onchange = (event) => {
const files = event.target.files;
console.log(files);
}
}
</script>
三:js将JSON格式转化为普通格式
1.样例:ajax请求后端,传回来的是JSON格式的数据,需要转化,使用JSON.parse(data);
$.ajax({
url: '/paddle/videoName',
type: 'GET',
success : function (data) {
var data = JSON.parse(data);
var videoShow = document.getElementById("videoShow");
videoShow.src = "../static/output/infer_video/" + data['name']
},
error: function (data) {
alert("预测失败,请重新上传");
}
})
四:js在子页面获取父页面的id
1JQuery
$("#id",parent.document)
$(window.parent.document).find("#id")
2.js
parent.document.getElement.getElementById("id)