无法使用getElementById

Not work with getElementById

本文关键字:getElementById      更新时间:2023-09-26

getElementById("VAL5"+i)无效。然后input3没有运行

我希望有了这个代码足够的信息来提供

错误在行的某个位置

/*数字输入*/

index=输入2_off对于(i=0;i<4;i++)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>
document.getElementById("VAL5"+i); 

或者你可以使用:

document.querySelector("VAL5"+i)

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

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