When to use the `#` symbol to get a DOM element?

前端 未结 4 1342
礼貌的吻别
礼貌的吻别 2020-12-04 01:39

Sometimes I need to get elements like this:

var object = document.getElementById(\'ObjectName\');

And sometimes like this:

var         


        
4条回答
  •  误落风尘
    2020-12-04 01:50

    The # is part of the ID selector in CSS. There are libraries and methods which support CSS selectors for selecting elements, such as jQuery and the native DOM methods querySelector and querySelectorAll.

    But in "traditional" DOM method, the # has no special meaning. getElementById('#ObjectName') would select an element which literally has the ID #ObjectName, i.e.

    .

提交回复
热议问题