gpt4 book ai didi

javascript - document.getElementById() 的返回类型是什么

转载 作者:行者123 更新时间:2023-12-03 23:30:44 27 4
gpt4 key购买 nike

此代码段中变量“元素”的类型是什么?
我以为它是一个数字(一个 ID 或其他东西),但现在,我不知道。
代码有效,但我不明白,为什么 var 元素可以在 for 循环中使用,就像数组一样。对此有什么解释吗?

   <script type="text/javascript">
function showAtrributes() {
var element = document.getElementById("videos");
var listAttributes = "";
for(var attribute in element) {
var valueOfAtrrib = element.getAttribute(attribute);
listAttributes = listAttributes + attribute + ": " + valueOfAttrib + "\n";
}
alert(listAttributes);
}
</script>

最佳答案

The getElementById() method returns the element that has the ID attribute with the specified value. [....] Returns null if no elements with the specified ID exists.



所以它返回一个 HTMLElement 对象

source

关于javascript - document.getElementById() 的返回类型是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com