js常用(包含,对时间格式的转化、点击事件的监听,form表单传数据,以及上传图片、文件,js将JSON格式转化为普通格式等初学者看,js在子页面获取父页面的id)

一: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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@黑夜中的一盏明灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值