我写js代码的时候,发现有一个元素没有通过document.getElementById而是直接使用,例如:
<style>
p{ width:100px; height:100px }
</style>
<p id="bt"></p>
<script>
var bt= document.getElementById('bt'); //实际上这行代码忘记写了
bt.style.background='red';
</script>
但是该元素的背景依然变红了,我接着把全部的document.getElementById删除(一共20多个)发现很多功能依然管用。
请问,这是为什么呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
部分浏览器支持id直接访问dom