所以我的HTML是:
<!DOCTYPE>
<head>
<script src="js/problem1.js"></script>
</head>
<body>
<input id="clickMe" type="button" value="Problem #1" onclick="problem1();" />
<script src="js/problem1.js"></script>
</body>我正在运行的脚本是:
var counter = 0
var total = 0
var start = prompt("Enter a number here to find the sum of all numbers that are:\n-Between 0 and your number\n-Divisible by 3 or 5")
function problem1(start) {
for (counter = 0; counter < start; counter++) {
if (counter < start) {
if (counter % 3 == 0) {
total += counter;
}
else if (counter % 5 == 0) {
total += counter;
}
}
else if (counter == start) {
if (counter % 3 == 0) {
total += counter;
alert(total);
}
else if (counter % 5 == 0) {
total += counter;
alert(total);
}
else {
alert(total);
}
}
}
}我遇到的问题是,现在脚本运行在页面加载上,并立即给我创建start变量的提示。输入整数并单击Ok后,提示窗口消失,什么也不会发生。我意识到脚本是独立运行的,因为它在<head>中被调用。问题是我不知道怎么称呼它,就像按一下按钮。我不想使用JQuery。我见过很多像this one这样的答案,但我正在寻找不需要JQuery的东西。我怎样才能做到这一点?
更新
谢谢你的帮助。现在我偶然发现了第二个问题。我无法让脚本运行或显示警报(不确定是哪个)
发布于 2016-10-28 01:15:11
将提示放在问题函数中
function problem1() {
var counter = 0
var total = 0
var start = prompt("Enter a number here to find the sum of all numbers that are:\n-Between 0 and your number\n-Divisible by 3 or 5")
for (counter = 0; counter < start; counter++) {
if (counter < start) {
if (counter % 3 == 0) {
total += counter;
alert(total);
}
else if (counter % 5 == 0) {
total += counter;
alert(total);
}
}
else if (counter == start) {
if (counter % 3 == 0) {
total += counter;
alert(total);
}
else if (counter % 5 == 0) {
total += counter;
alert(total);
}
else {
alert(total);
}
}
}
}<!DOCTYPE>
<head>
<script src="js/problem1.js"></script>
</head>
<body>
<input id="clickMe" type="button" value="Problem #1" onclick="problem1();" />
<script src="js/problem1.js"></script>
</body>
https://stackoverflow.com/questions/40296436
复制相似问题