JavaScript 常用代码 • 整理

本文介绍了JavaScript的一些基本函数,如setTimeout和setInterval,用于延时和周期性执行任务。还展示了如何使用JavaScript操作DOM,如改变class和属性。此外,详细给出了fetch进行异步HTTP请求的示例,用于上传数据到后台。最后,提到了feather库在前端显示图标的用法及其动态更改的方法。
摘要由CSDN通过智能技术生成

一、常用函数

 // 延时3秒执行方法buttonIsCheck('finger')
setTimeout("buttonIsCheck('finger')","3000");
// 间隔6秒执行方法buttonIsCancel('finger')
setInterval("buttonIsCancel('finger')","6000");
// 滚动条 · 竖向移动index的长度
function setScollTo(index) {
	window.scrollTo(0, index);
}
// 字符串截取
var str=“Hello happy world!”
str.slice(6); //从第6位开始截取到末尾 result:happy world!
str.slice(6,11); // 截取6到11位 result:happy
str.slice(-2); // 截取最后两位

二、JavaScript常用操作

// 重置class
document.getElementById(id).className = "btn-warning btn-3d-war";
// 修改属性
document.getElementById(id+"Span").setAttribute("data-feather", "check");
// textContent属性返回或设置当前元素中的文本内容
document.getElementById(id).textContent = "一体化采集 · 消息提示@";

三、fetch 异步访问后台服务示例

当前示例后台返回JSONString

/*
 * 人像数据上传 · 把获取到的人像照片存储到服务器
 * fileName:含后缀的文件名
 * fileStr :人像采集程序返回的人像照片数据
 * index:00-->二代证照;01-->正面照;02-->左侧照;03-->右侧照
 */
function uploadAfisImage(index, fileName, fileStr) {
	var barcode = "53012568202307050099";
	var psnId = 0;
	
	fetch("/${sessionScope.project}/fileUpload/uploadPsnImage", {
		method: "POST",
		headers: {'Content-Type': 'application/json'},
		body: JSON.stringify({
			"psnId": psnId,
			"psnNo": barcode,
			"fileName": fileName,
			"imageType" : index,
			"fileStr": fileStr
		})
	}).then(function(response){
			if(response.ok){
				console.log("成功访问人像照片后台接收接口!");
				response.json().then((data) => {
					console.log(data); // 后台返回的json字符串
				})
			}
	}).catch(function(error){
			console.error('Error:', error);
			console.log('There has been a problem with your fetch operation: ', error.message);
	});
}

四、feather(图标)常用操作

1.引入依赖

<script	type="text/javascript"
src="/${sessionScope.project}/feather/feather.min.js"></script>

<script type="text/javascript">
// feather显示图标
feather.replace({
	'aria-hidden' : 'true'
});
</script>

2.动态更改feather图标

// 定位并修改属性
document.getElementById(id+"Span").setAttribute("data-feather", "x");
feather.replace(); //缺少此代码则图标不会进行更改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值