我有这个代码工作:
$("#downloadBtn").click(function() {
weeks = getWeeks(startDate.val(), endDate.val()); // array like [[startDay1, endDay1], [startDay2, endDay2], ...]
// start downloading the data
var promiseArray = [];
for (i=0; i< weeks.length; i++) {
var _promise = Q.defer();
fetchDataWeek( weeks[i][0], weeks[i][1], _promise );
promiseArray.push(_promise) // Push this promise into the array
}
Promise.all(promiseArray).then( function () { // Wait for all promises to resolve
var rows = [headers];
for (i=0; i < promiseArray.length; i++) {
rows.push(promiseArray[i]);
}
exportToCsv( fileName, rows );
})
});
function fetchDataWeek( startDay, endDay, _promise ) {
url = "https://api" + startDay + endDay + ".json";
$.ajax({
url: url,
success: function(result){
var weekRows = parseHistory(result);
_promise.resolve(weekRows);
},
error: function (error) {
_promise.reject(error) // rejecting it in case of error
}
});
}
// Extract all data from a query response
function parseHistory(data) {
var weekRows = [];
var days = data.history.days;
for (var i = 0; i < days.length; i++) {
dayRows = formatDay( days[i] );
for (var j= 0; j < dayRows.length; j++) {
weekRows.push(dayRows[j]);
}
}
return weekRows;
}
现在我试图划分创建一个js文件并将脚本代码放入其中。 现在我的代码是:
html文件
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
<img id='qrcode' src=''>
<button onclick="newQR()">Gerar QRcode</button>
<script> function newQR() {
var x = Math.floor((Math.random() * 99999999999999999) + 1);
document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x + "&size=[500]x[500]&ecc=L&qzone=4&format=png";
}
</script>
</body>
javascript文件(它位于名为js的粘贴内)
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
<img id='qrcode' src=''>
<button onclick="newQR()">Gerar QRcode</button>
<script type = "text/javascript" src = "js/script.js"></script>
</body>
</html>
为什么它不再起作用了?
答案 0 :(得分:0)
我认为这是因为你在调用函数后链接了你的javascript文件。我宁愿在头脑中提供javascript参考。
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript" src = "js/script.js"></script>
</head>
<body>
<img id='qrcode' src=''>
<button onclick="newQR()">Gerar QRcode</button>
</body>
</html>
答案 1 :(得分:0)
您的javascript是相对而非绝对链接的:
<script type = "text/javascript" src = "js/script.js"></script>
到
<script type = "text/javascript" src = "/js/script.js"></script>
通过导航到/js/script.js
,验证您的JS在浏览器中是否可见。如果它仍然不起作用,请检查控制台是否有错误(右键单击&gt;检查元素)