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

标签 javascript

此代码段中变量“元素”的类型是什么?
我以为它是一个数字(一个 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/

相关文章:

php - 如何定义虚拟根?

javascript - 动态创建对象实例的约定是什么?

javascript - 可以将事件添加到 d3 调度程序吗?

javascript - Node.js、TypeScript、JavaScript 和 Angular 之间的连接

javascript - 如何并行 api 调用并保持列表中响应的顺序以供 ui 呈现(RxJS Observables)

在 Spring 框架中进行用户验证后,Javascript 将不会移动到下一个网页

javascript - 调用名称保存在字符串中的 JavaScript 函数

javascript - AngularJS - 将 typeahead 与 ngTagsInput 结合使用

javascript - 在页面上列出博主帖子,带有图像并使用 CSS 进行风格化

javascript - 如果一个 javascript 变量位于类之外,但位于模块的闭包内,它是私有(private)的吗?