我之前得到了一些关于这段代码的帮助,但我遇到了另一个问题。如何修改每个函数内的变量并在该范围之外使用它们?
这是我的代码的简单解释:对于每个网页,获取元素的内容并将它们插入到数组中。在此之后,操作数组以创建消息并向浏览器发送警报。
HTML
<div id="insert"></div>
Javascript
$(document).ready(function() {
userid = 17320;
courseid = 4745;
allLessonsArray = script(userid, courseid);
function script(userid, courseid) {
var lessonInTopicSectionArray = new Array();
lessonInTopicSectionArray['Floods, external'] = 376;
var allLessonsArray = new Array();
$.each(lessonInTopicSectionArray, function(name, lesson) {
lastAttempt = 0;
url = '---------/learn/mod/lesson/report.php?id=' + lesson + '&action=reportdetail&userid=' + userid + '&try=' + lastAttempt;
var lessonArray = new Array();
$.get(url, function(data) {
var page = $(data);
$('tr.r1.lastrow td.cell.c0').html(page).each(function(index, content) {
score = parseInt(content.substr(content.length - 1));
lessonArray[index] = score;
});
allLessonsArray[name] = lessonArray;
});
});
return allLessonsArray;
}
var message = "";
$.each(allLessonsArray, function(name, lessonArray) {
if (!message) {
message = "<li>Lesson <b>" + name + "</b> scores are:</li>";
}
else {
message = message + "<li>Lesson <b>" + name + "</b> scores are:</li>";
}
probability = lessonArray[0];
humanImpact = lessonArray[1];
infrastructureImpact = lessonArray[2];
communityImpact = lessonArray[3];
training = lessonArray[4];
preparedness = lessonArray[5];
message = message + "<ul><li>Probability score: " + probability + "</li><li>Human Impact score: " + humanImpact + "</li><li>Public Health Infrastructure Impact score: " + infrastructureImpact + "</li><li>Community Impact score: " + communityImpact + "</li><li>Training score: " + training + "</li><li>Preparedness score: " + preparedness + "</li></ul>";
risk = (probability / 3) * ((humanImpact + infrastructureImpact + communityImpact + training + preparedness) / 15);
message = message + "<p>Risk of <b>" + name + "<b> is" + risk + ".</p>";
});
$('#insert').html(message);
});