不适用于getElementById

时间:2014-09-10 20:18:46

标签: javascript getelementbyid

不工作getElementById(" VAL5" + i)。 然后input3没有运行

我希望用这段代码提供足够的信息

错误在行

/ * DigitalInput * /

index = input2_offs             对于(I = 0; I< 4;我++)     getElementById(" VAL5" + i).src = valueArray [input2_offs [i]] ==" 0" ? ' GIF / dot_red.gif' :' gif / dot_gr.gif'

问题在哪里


enter code here

<!--

var req = null;
req = AjaxInit();
var PostBuffer = new Array;

var Trace;

/* number of channels per sensor input type */
var InputChannels = 4;  /* max 4 digital inputs          */

var names = new Array();
var units = new Array();
var offs = new Array();
var gain = new Array();
var value = new Array();

var input2_offs = 25;
var input3_offs = 26;

/*
    this function is called on body load and later every 5s
    request a new set of data from the controller
*/
function UpDate()
{
    var x = Math.random()*1000;  /* add random number to the url to avoid caching the content in the browser */
    x=x.toFixed(3);
    AjaxGetRequest("/GetState.csv"+"?a="+x,UpdateTraceBuffers);
    return false;
}

/*
    call back routine when the controller has send the data
    this function separates all items in separate arrays as :
    names, units, offsets , gains  and the raw values
*/
function UpdateTraceBuffers()
{

    Trace = req.responseText.split('\n');
    names = Trace[TraceNameOffs].split(',');
    units = Trace[TraceUnitOffs].split(',');
    offs = Trace[TraceOffs].split(',');
    gain = Trace[TraceGain].split(',');
    value = Trace[TraceDataoffs].split(',');
    UpdateWebSite();                         /* user routine , update all items */
    if(timerHandle == 0)
    {
        timerHandle = window.setInterval("UpDate()", 5000);
    }
}

/* DigitalInput */

    index = input2_offs;
            for(i=0;i<4;i++)
    getElementById("VAL5"+i).src = valueArray[input2_offs[i]] == "0" ? 'gif/dot_red.gif' : 'gif/dot_gr.gif';

    index = input3_offs;
    v = (parseFloat(offs[index]) + parseFloat(gain[index])* parseFloat(value[index])).toFixed(0);
            getElementById("SENS_VAL6").innerHTML = v;

//-->


<td class="mytd"><div align="center"><img src="gif/dot_red.gif"
 width="24" height="24" id="VAL5"></div></td>  
<td id="SENS_VAL6" class="mytd" align="right">&nbsp;</td>

1 个答案:

答案 0 :(得分:0)

document.getElementById("VAL5"+i); 

或者您可以使用:

  

document.querySelector( “VAL5” + I)

适用于所有现代浏览器(IE8 +)。 https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector

实际上在for循环和if语句等中使用{},这是js中推荐的做法。