我的JS代码如何在没有我明确缓存它们的情况下访问DOM元素?

时间:2015-12-18 23:17:58

标签: javascript html caching

有人可以帮我理解为什么textarea在没有先缓存的情况下进行更新

在我的HTML中,我有一个

<textarea id="inputData"></textarea>

以及对我的js文件的引用:

(function(){
    "use strict"
    var analyzer = {
        text : [],
        init : function(){
            this.cacheDOM():
            this.render();
        },
        cacheDOM : function(){
           // Here is where I want to cache a portion of my DOM 
        },
        render : function(){
            inputData.value         = "my  data";
        }

    }// End analyzer

    analyzer.init();

})();

结果是textarea被js文件(渲染函数)更新,我不知道/理解为什么在没有我首先缓存DOM的情况下发生这种情况......

PS:这与“id”属性相关,而不是使用“class”es。

1 个答案:

答案 0 :(得分:1)

所有带有id的html元素都作为属性添加到javascript中window对象。 See this for more details.

我认为原因是向后兼容。欢迎来到前端开发的世界,