javascript 怎样把闭包里处理得到值传到闭包外面,使闭包外面的变量af的值和闭包里af的值同步变化、相等? 30
<!DOCTYPEHTML><html><head><title>Canvas</title><styletype="text/css">.thumb{height:75...
<!DOCTYPE HTML>
<html>
<head>
<title>Canvas </title>
<style type="text/css">
.thumb {
height: 75px;
border: 5px solid #000;
margin: 20px 10px 0 0;
}
</style>
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
var af = 2;//闭包外的af
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = function test(e) {
var df = 60;
var dataUri = event.target.result;
af = 10 + df + i; //闭包里的af
img = new Image();
img.src = dataUri;
document.body.appendChild(img);
alert(af);// 弹出闭包里af的值
}
reader.readAsDataURL(f);
}
alert(af); //弹出闭包外的af的值
}
</script>
</body>
</html>
怎样把闭包里处理得到值传到闭包外面,使闭包外面的变量af的值和闭包里af的值同步变化、相等?? 展开
<html>
<head>
<title>Canvas </title>
<style type="text/css">
.thumb {
height: 75px;
border: 5px solid #000;
margin: 20px 10px 0 0;
}
</style>
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
var af = 2;//闭包外的af
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = function test(e) {
var df = 60;
var dataUri = event.target.result;
af = 10 + df + i; //闭包里的af
img = new Image();
img.src = dataUri;
document.body.appendChild(img);
alert(af);// 弹出闭包里af的值
}
reader.readAsDataURL(f);
}
alert(af); //弹出闭包外的af的值
}
</script>
</body>
</html>
怎样把闭包里处理得到值传到闭包外面,使闭包外面的变量af的值和闭包里af的值同步变化、相等?? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询