JavaScript不用获取元素,直接通过id操作dom

任意写一个div,任意设置id   test

打印出window对象,发现window.document.all下面挂载了test

使用document.getElementById('test')获取id为test的元素

因为window是默认的全局对象,所以可以简写,原来是window.document.getElementById()

console.log(window.document.all.test===window.document.getElementById('test'))  //true

posted @ 2022-01-21 02:40  poder  阅读(243)  评论(0)    收藏  举报