使用JS <input type=“file“ />实现简单的选择图片并实时显示

html:

<img style="width: 200px" id="img" src="">
<input type="file"  onchange="displayimg(this)"  name="myfile">

JavaScript:

function displayimg(input){
	//获取第一个文件对象 (如果有多张时可使用循环files数组改变多个<img>的 src值)
	var file = input.files[0];
	//判断当前是否支持使用FileReader
	if(window.FileReader){
		//创建读取文件的对象
		var fr = new FileReader();
		//以读取文件字符串的方式读取文件 但是不能直接读取file 
		//因为文件的内容是存在file对象下面的files数组中的
		//该方法结束后图片会以data:URL格式的字符串(base64编码)存储在fr对象的result中
		fr.readAsDataURL(file);
		fr.onloadend = function(){
			document.getElementById("img").src = fr.result;
		}
	}
}

效果展示:

在这里插入图片描述

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码来实现您的要求: ```html <style> #entryDetail { width: 100%; table-layout: fixed; } th, td { padding: 10px; text-align: center; } th:first-child, td:first-child { width: 10%; } th:nth-child(2), td:nth-child(2) { width: 15%; } th:nth-child(3), td:nth-child(3), th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5) { width: 12%; } th:nth-child(6), td:nth-child(6) { width: 20%; } th:last-child, td:last-child { width: 16%; } tbody tr:hover { background-color: #f5f5f5; } </style> <script> function deleteTableRow(btn) { var row = btn.parentNode.parentNode; row.parentNode.removeChild(row); } </script> <table id="entryDetail"> <thead> <tr> <th>操作</th> <th>提单号/运单号</th> <th>一程日期</th> <th>二程日期</th> <th>预计到货日期</th> <th>海船公司</th> <th>放箱日期</th> <th>放箱单上传</th> </tr> </thead> <tbody> <tr> <td><input type="button" name="comment" onclick="deleteTableRow(this)" value="删除"></td> <td><input type="text" name="comment"></td> <td><input type="date" name="date1"></td> <td><input type="date" name="date2"></td> <td><input type="date" name="date3"></td> <td><input type="text" name="comment1"></td> <td><input type="date" name="date4"></td> <td><input type="file" name="attachment"></td> </tr> </tbody> </table> ``` 此代码将表格的宽度设置为100%,并使其表格布局固定。它还将表头和单元格的填充和文本对齐方式设置为居中。每个表头和单元格都有一个宽度百分比,以便在表格宽度变化时自适应。还添加了一个鼠标悬停样式以提高用户体验。在代码的底部,有一个JavaScript函数deleteTableRow(btn),它将从表格中删除所在行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值