for循环正常工作与console.log但不是innerHTML? [英] for loop works fine with console.log but not innerHTML?

查看:247
本文介绍了for循环正常工作与console.log但不是innerHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是非常新的JavaScript,所以原谅我,如果答案似乎明显...

这是我的代码是在点击一个按钮在体内

 函数q(){

var A = document.getElementById(time_one )。值;
var B = document.getElementById(time_two)。value;
var C = document.getElementById(post_number)。value;

var D =(B - A)/ C; (var x = A; x
document.getElementById(q_box)。innerHTML = x +< b
$ b< ; / /






$ b

我很确定错误在for循环中...输出应该是A和B之间的数字列表,并且将innerHTML =更改为innerHTML + =也没有工作吗?

解决方案

  function q(){

var A = + document.getElementById ( time_one)值;
var B = + document.getElementById(time_two)。value;
var C = + document.getElementById(post_number)。value;

var D =(B - A)/ C; (var x = A; x
document.getElementById(q_box)。innerHTML + = x +b
$ b < br />;





$ b

你应该转换 int 中的值,您应该使用 + =


hello i'm very new to javascript so forgive me if the answer seems obvious...

this is my code which is executed at the click of a button in the body

function q() { 

    var A = document.getElementById("time_one").value;
    var B = document.getElementById("time_two").value;
    var C = document.getElementById("post_number").value;

    var D = (B - A) / C;

    for ( var x = A; x < B; x = x + D ) {

    document.getElementById("q_box").innerHTML = x + "<br />";

    }


}

i'm pretty sure the error is in the for loop... the output is supposed to be a list of numbers between A and B. and changing the innerHTML = to innerHTML += hasn't worked either?

解决方案

function q() { 

    var A = +document.getElementById("time_one").value;
    var B = +document.getElementById("time_two").value;
    var C = +document.getElementById("post_number").value;

    var D = (B - A) / C;

    for ( var x = A; x < B; x = x + D ) {

    document.getElementById("q_box").innerHTML += x + "<br />";

    }

}

You should convert the values in int and you should use +=

这篇关于for循环正常工作与console.log但不是innerHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆