我无意中发现只需输入元素的 id 即可访问元素,例如:
<input id="test" value="testValue"/>
<script>
console.log(test.value); //testValue
</script>
有人能指出我这个功能来自哪里吗? (用谷歌搜索这样的东西真的很难)
最佳答案
window[name]
Returns the indicated element or collection of elements.
As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the Web platform, for example. Instead of this, use document.getElementById() or document.querySelector().
关于javascript - javascript中通过ID访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708490/