4

querySelector() returns undefined string when trying to access numeric value as the ID of an element. I would like to use querySelector() method over others, because it is more flexible. Any solutions to this problem would be a great help.

var foo = document.querySelector('#1');
console.log(foo);

jsFiddle

4

2 回答 2

6

奇怪..无论如何这:document.querySelector('[id="1"]')正在工作。小提琴:http: //jsfiddle.net/wnd7K/2/

于 2013-08-04T05:10:40.097 回答
0

重命名元素的 id 以使它们以字母开头可以解决您的问题,例如: <div id="a1"></div>

于 2018-06-05T12:02:30.377 回答