Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在循环中更改时输出变量列表?_Javascript - Fatal编程技术网

Javascript 如何在循环中更改时输出变量列表?

Javascript 如何在循环中更改时输出变量列表?,javascript,Javascript,这是我的代码,我目前正试图使用document.getElementById创建列表,但它只输出变量v的最后一个值。我是编程新手,所以我不知道如何让它以正确的方式显示奇数列表 <!DOCTYPE HTML> <html lang="en-us"> <head> <meta charset="utf-8"> <title>Sum of odd numbers between one and the input</title>

这是我的代码,我目前正试图使用document.getElementById创建列表,但它只输出变量v的最后一个值。我是编程新手,所以我不知道如何让它以正确的方式显示奇数列表

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Sum of odd numbers between one and the input</title>
<script type="text/javascript">
/* Defining Table
 * Input: User inputs a number (n)
 * Processing: add all the odd intergers between 1 and the input (n)
 * Output: The sum
 */
function oddSum() {
var number = document.getElementById("input").value;
var n = parseFloat (number);
var sum = 0;


for (v = 1; v <= n ; v +=2){

document.getElementById("v").innerHTML = v;

sum += v;

}

document.getElementById("outputDiv").innerHTML = sum;
}
</script>
</head>

<body>                             
Input a number <input type="text" id="input">    
<button type="button" onclick="oddSum()">Sum of odd</button>
<br> <br> 
Here are the odd numbers. <div id="v"></div>
<br> Here is the sum of the odd numbers. <div id="outputDiv"></div>


</body>       
</html>

一和输入之间的奇数之和
/*定义表
*输入:用户输入一个数字(n)
*处理:将1和输入(n)之间的所有奇数整数相加
*输出:总和
*/
函数oddSum(){
var number=document.getElementById(“输入”).value;
var n=parseFloat(数字);
var总和=0;

对于(v=1;vinnerHTML替换元素的内部html,它不会将新内容附加到您已经拥有的内容中

也许试试

document.getElementById("v").innerHTML += v;

还请检查此项::)

不完全相同,但我用JS小提琴来演示逻辑。要从计数中排除数字1,对吗?只需将提示符替换为输入元素,将警报替换为输出div innerhtml

var input=prompt('请输入一个数字');
var合计=0;

对于(var i=1;i来说,要回答为什么只获取最后一个元素的问题,innerHTML方法会获取目标元素并清除它,然后用通过变量分配的内容替换它的内容。因此,最后您将得到的是最后一个元素


我相信您正在尝试构建DOM元素。要做到这一点,您必须使用另一种方法。

感谢这一方法的有效性,我觉得应该有一种不同的方法来实现它。嗯,我认为您的for循环没有任何问题。您认为问题出在哪里?我想您可以将所有输出存储在一个字符串中,然后更改innerHTML…我想这没什么问题,我真的不明白在=之前添加a+是如何工作的。你能解释一下吗?谢谢。这是一种速记,在许多编程语言中都很常见。而不是编写document.getElementById(“v”).innerHTML=document.getElementById(“v”).innerHTML+v;您使用+=是我想排除1。感谢这有助于我更好地理解。我很高兴它帮助了Jice。如果它回答了您的问题,请将其标记为正确答案,以便未来的旁观者更容易找到解决方案。
var input = prompt('please enter a number.');
var total = 0;
for(var i=1;i<input;i+=2){
    total += i;
}
alert(total-1);