document.getElementById not working even if I ve ID present in my css

前端 未结 2 1547
粉色の甜心
粉色の甜心 2020-12-20 08:20

I tried searching but didnt get a valid answer to my question. I always get \"null\" when I call document.getElementById(\"#\"+id) and it alerts Document

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 08:46

    While you're using JavaScript, you don't use # sign to reference the ID of the element.

    Your code for

    alert("#"+id);
    

    Would be

    alert(id);
    

    You need # Number sign, only when working with CSS or jQuery.

提交回复
热议问题