这篇文章主要讲解了“JS+HTML5实现图片在线预览功能的代码怎么写”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JS+HTML5实现图片在线预览功能的代码怎么写”吧!
具体代码如下
<!DOCTYPE html>
<html>
<head>
<title>HTML5图片预览</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://img9.tongzhuo100.com/js/jquery-1.7.2.min.js"></script>
<style>
.hide
{
display:none;
}
</style>
</head>
<body>
<h4>请选择一张JPG/GIF的图片</h4>
<form name="form0" id="form0" >
<input type="file" name="file0" id="file0" multiple="multiple" />
<br><br><img src="" id="img0" width="120" class="hide">
</form>
<script>
$("#file0").change(function(){
var objUrl = getObjectURL(this.files[0]) ;
console.log("objUrl = "+objUrl) ;
if (objUrl)
{
$("#img0").attr("src", objUrl);
$("#img0").removeClass("hide");
}
}) ;
function getObjectURL(file)
{
var url = null ;
if (window.createObjectURL!=undefined)
{ // basic
url = window.createObjectURL(file) ;
}
else if (window.URL!=undefined)
{
// mozilla(firefox)
url = window.URL.createObjectURL(file) ;
}
else if (window.webkitURL!=undefined) {
// webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
</script>
</body>
</html>
感谢各位的阅读,以上就是“JS+HTML5实现图片在线预览功能的代码怎么写”的内容了,经过本文的学习后,相信大家对JS+HTML5实现图片在线预览功能的代码怎么写这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!