【发布时间】:2014-06-13 04:18:52
【问题描述】:
我觉得我很接近了。
我想要做的是单击一个按钮,浏览一个文件并让它立即显示在浏览器中。到目前为止 - 当我单击按钮时,我可以浏览我的文件,但我在浏览器中得到的只是一个损坏的图像图标。
<!DOCTYPE html>
<html>
<head>
<script>
function loadImage()
{
var myFiles = document.getElementById("files");
document.getElementById("images").innerHTML = "<img src='" + myFiles.files.src +"'>";
}
</script>
</head>
<body>
<div>
<input id="files" type="file" name="files" accept="image/*" onchange="loadImage();">
</div>
<div id="images">
Images will go here...
</div>
</body>
</html>
看起来应该很简单,但我似乎无法让它工作。有人可以纠正我吗?提前致谢!
[编辑] 感谢您的快速回复,user3030089!
我是根据我上学期在学校的一个项目来做这个的。它基本上做了我想要的,但它是为一个数组做的。对于我现在正在做的事情,我只想要一个图像 - 而不是一个数组,这样我就可以摆脱那些 for 循环等。
<!DOCTYPE html>
<html>
<head>
<script>
function loadImage()
{
myImages = new Array();
var myFiles = document.getElementById("files");
for (var i=0; i < myFiles.files.length; i++)
{
myImages[i] = myFiles.files[i].name;
}
var theImages = document.getElementById("images");
theImages.innerHTML = ""; // Clearout the image area.
for (var i=0; i < myFiles.files.length; i++)
{
"<img id='pic" + i + i + ";'" + " src='" + myImages[i] +
"'" + " width = '400' " + " /> ";
}
}
</script>
</head>
<body>
<div>
<input id="files" type="file" name="files[]" accept="image/*" onchange="loadImage();" multiple />
</div>
<div id="images" style="position:relative; ">
Images will go here...
</div>
</body>
</html>
我想做的只是简化它,使它只适用于一张图像。
再次感谢!
【问题讨论】:
标签: javascript html image button load