当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 用js实现预览待上传的本地图片

Javascript
javascript据option的value值快速设定初始的selected选项
document.open() 与 document.write()的区别
javascript实现轮显新闻标题链接
一段效率很高的for循环语句使用方法
使一个函数作为另外一个函数的参数来运行的javascript代码
javascript引用对象的方法代码
javascript下给元素添加事件的方法与代码
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
js 事件小结 表格区别
用js实现计算代码行数的简单方法附代码
收藏的js表单验证控制代码大全
提供一款很不错的万年历查询
用javascript实现计算两个日期的间隔天数
用javascript实现在小方框中浏览大图的代码
用js实现上传图片前的预览(TX的面试题)
用javascript实现li 列表数据隔行变换背景颜色
(currentStyle)javascript为何有时用style得不到已设定的CSS的属性
总结AJAX相关JS代码片段和浏览器模型
超强推荐的js编程中的简洁写法收集
javascript 函数式编程

Javascript 中的 用js实现预览待上传的本地图片


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 248 ::
收藏到网摘: n/a

<form name="form5" id="form5" method="post" action="#">
<input type="file" name="file5" id="file5" onchange="preview5()"/>
</form>
<script type="text/javascript">
function preview5(){
var x = document.getElementById("file5");
if(!x || !x.value) return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i;
if(patn.test(x.value)){
var y = document.getElementById("img5");
if(y){
y.src = "file://localhost/" + x.value;
}else{
var img=document.createElement("img"); img.setAttribute("src","file://localhost/"+x.value);
img.setAttribute("width","120");
img.setAttribute("height","90");
img.setAttribute("id","img5");
document.getElementById("form5").appendChild(img);
}
}else{
alert("您选择的似乎不是图像文件。");
}}
</script>