出于某种原因,当我尝试输出一组连接的字符串和变量时,输出我得到了这个输出:
Arr! I, function (a,b){return new n.fn.init(a,b)} have plunderedundefined gold
doubloons and sold them for a total of $NaN time to buy me a new cannon!
这是我的代码:
HTML:
<!DOCTYPE HTML>
<html>
<head>
<title>Conversions</title>
<meta charset="UTF-8">
<script src="../../testFile_files/jquery.min.js.download"></script>
<script src="conversions.js"></script>
</head>
<body>
<div id="output"></div>
</body>
</html>
使用Javascript:
var strName = "";
var strDoubloons = "";
$(document).ready( function(strName) {
var strAskName = "Arrr, what is your Pirate name?";
var strDefaultName = "Captain Blackbeard";
strName = prompt(strAskName, strDefaultName);
return strName;
});
$(document).ready( function(strDoubloons) {
var strAskDoubloons = "Arr, how many gold doubloons have ye'plundered?";
var strDefaultDoubloons = "100";
strDoubloons = prompt(strAskDoubloons, strDefaultDoubloons);
return strDoubloons;
});
$(document).ready ( function(strName, strDoubloons){
intDoubloons = parseInt(strDoubloons);
intProfit = intDoubloons * 287;
var message = "Arr! I, " + strName + " have plundered" + strDoubloons +
" gold doubloons and sold them for a total of $" + intProfit + " time to buy me a new cannon!";
var display = document.getElementById("output");
display.textContent = message;
});
答案 0 :(得分:0)
It's a bad practice to use global variables like this. Better spearate tour code into functions that get and return values and have as little side effects as possible. This way it is much easier to reason about the code. As for why it didn't work before is because you replaced the onready
event handler of the document every time and it only executed the last.
function getName() {
var strAskName = "Arrr, what is your Pirate name?";
var strDefaultName = "Captain Blackbeard";
return prompt(strAskName, strDefaultName);
}
function getDoubloons() {
var strAskDoubloons = "Arr, how many gold doubloons have ye'plundered?";
var strDefaultDoubloons = "100";
return prompt(strAskDoubloons, strDefaultDoubloons);
}
function printInfo(strName, strDoubloons) {
intDoubloons = parseInt(strDoubloons);
intProfit = intDoubloons * 287;
var message = "Arr! I, " + strName + " have plundered " + strDoubloons +
" gold doubloons and sold them for a total of $" + intProfit + " time to buy me a new cannon!";
var display = document.getElementById("output");
display.textContent = message;
}
$(document).ready(function() {
printInfo(getName(), getDoubloons());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML>
<html>
<head>
<title>Conversions</title>
<meta charset="UTF-8">
<script src="../../testFile_files/jquery.min.js.download"></script>
<script src="conversions.js"></script>
</head>
<body>
<div id="output"></div>
</body>
答案 1 :(得分:-1)
在document.ready之外编写函数并在
中调用它们function func1(strName) {
var strAskName = "Arrr, what is your Pirate name?";
var strDefaultName = "Captain Blackbeard";
strName = prompt(strAskName, strDefaultName);
return strName;
}
function func2(strDoubloons) {
var strAskDoubloons = "Arr, how many gold doubloons have ye'plundered?";
var strDefaultDoubloons = "100";
strDoubloons = prompt(strAskDoubloons, strDefaultDoubloons);
return strDoubloons;
}
function func3(strName, strDoubloons){
intDoubloons = parseInt(strDoubloons);
intProfit = intDoubloons * 287;
var message = "Arr! I, " + strName + " have plundered" + strDoubloons +
" gold doubloons and sold them for a total of $" + intProfit + " time to buy me a new cannon!";
var display = document.getElementById("output");
display.textContent = message;
}
$( document ).ready(function() {
// Call your start up function
});
答案 2 :(得分:-1)
我会这样做https://jsfiddle.net/n7j5xfsf/
将所有代码放在一个文档就绪调用中,没有必要用文档就绪调用来包装每个函数。
id Associated id
AA12345 06-Jan-2017 12:00:00 AM 23-Jan-2017 11:59:59 AM AA12345, AA234567
AA12345 24-Jan-2017 12:00:00 AM 31-Dec-3030 11:59:59 PM AA12345, AA234567, AB56789