不能使用getElementById和var访问id

can't access id with getElementById and a var

本文关键字:var 访问 id getElementById 不能      更新时间:2023-09-26

我有一个问题,我不能得到id工作:

var charCodeRange = {
 start: 65,
 end: 90
}
character = charCodeRange.start
nbr = 1;
id_case = String.fromCharCode(character) + nbr

当我使用

document.getElementById(id_case).onmouseover= myfunction()

它不工作。我知道我的函数是有效的因为当我输入id_case = "A1"

一个想法?

代替:

document.getElementById(id_case).onmouseover= myfunction()

:

document.getElementById(id_case).onmouseover = myfunction;

我做了一个测试,它正在工作,如果我把这个:

document.getElementById("A1").style.backgroundColor = "#E8DDFF";

而不是if

stringtest = "A1"                          
document.getElementById(stringtest).style.backgroundColor = "#E8DDFF";
使用