目的:用file控件选择电脑中的图片,然后通过js即时在网页中显示所选择的图片。
已有:<img id="img" src="" />
<input id="chooseimg" type="file" onpropertychange="showpic()" />
function showpic(){
document.getElementById("img").scr = document.getElementById("chooseimg").value
没有能显示出来。
已有:<img id="img" src="" />
<input id="chooseimg" type="file" onpropertychange="showpic()" />
function showpic(){
document.getElementById("img").scr = document.getElementById("chooseimg").value
没有能显示出来。
document.getElementById("img").src = document.getElementById("chooseimg").value
}onchange="showpic()"
<input id="chooseimg" type="file" onchange="showpic(this)" /><br>
<img id="img" src="" /><script>
function showpic(t){
document.getElementById("img").src = t.value
}
</script>
我感觉
document.getElementById("img").src = document.getElementById("chooseimg").value
这个赋值完后应该有个语句刷新一下。它都会显示新的出来。问题是这怎么写?
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function preview(file, img){
var nImg = document.createElement('img');
nImg.id = img.id;
nImg.src = 'file:\/\/\/' + file.value.replace(/\\/g, '\/');
img.parentNode.replaceChild(nImg, img);
}
</script>
<input type="file" onchange="preview(this, $('test'))"/><img id="test" />