getElementById(' variableName')循环中断

时间:2014-04-03 15:08:25

标签: javascript

/*
 * Helper Functions
 */
function setAttributes(el, attrs) {
  for(var key in attrs) {
    el.setAttribute(key, attrs[key]);
  }
}

/*
 * Create div(s) from Object with style properties
 */
Layer.prototype.createEl = function(object, target, className) {
    count = 1;
    for(e in object) {
        var newElement = document.createElement("div")
        newElement.id = e;
        setAttributes(newElement, { 
          "class" : className, 
          "data-name" : e, 
          "data-order" :  count
        });

        for(x in object[e]) {
            newElement.style[x] = object[e][x];
        }

        target.appendChild(newElement);
        count++;
    }
}


if (typeof authorized !== "undefined" && app.identify()) 
{
    app.createEl(app.identify().containers, document.body, "layerFrame");
    for(e in app.identify().containers) {
        app.createEl(app.identify().framing, document.getElementById(e), "framework");
    }
}

app.identify()返回一个JSON对象。 .containers部分如下所示:

"containers" : {
    "master" : {
        "position" : "fixed",
        "width" : "150px",
        "height" : "200px",
        "bottom" : "0px",
        "right" : "100px",
        "background" : "yellow"
    },
    "response" : {
        "display" : "none",
        "position" : "fixed",
        "width" : "150px",
        "height" : "200px",
        "bottom" : "0px",
        "right" : "100px",
        "background" : "teal"
    }
},

中断的区域是我调用app.createEl函数的第3行代码。特别是getElementById(e)部分。

如果我在console.log' e',则返回" master"和#34;回应"如预期的那样。

如果我在document.getElementById(" master")而不是document.getElementById(e)中硬编码,那么一切都按照我想要的方式工作。

但是当我用' e'运行时,我得到" Uncaught TypeError:无法调用方法' appendChild' of null"

typeof(e)=一个字符串,当我使用getElementById(e)时为什么会中断?

ANSWER

不确定这是否与任何人相关,但问题是调用该函数 createEl(object,target,className) 并直接传入target.getElementById()作为目标参数。我所做的就是将document.getElementById()分配给一个变量,然后调用createEl()并传入该变量作为目标参数:

for(e in app.identify().containers) 
{
    tmp = document.getElementById(e);
    app.createEl(app.identify().framing, tmp, "framework");
}

1 个答案:

答案 0 :(得分:1)

好吧,伙计,我已经创建了FIDDLE来模拟你所说的内容,但是document.getElementById(e)在这里工作得非常好。 < / p>

如果你告诉我们什么是&#34; app.identify()。框架&#34;事情,我可以尝试模拟你正在做的事情。

编辑:

在您展示了框架内容之后,我已经更新了FIDDLE,并且它仍然运行良好。您的app.identity()可能不是您期望的Javascript对象。

var json =  {
    "containers" : {
        "master" : {
            "position" : "fixed",
            "width" : "150px",
            "height" : "200px",
            "bottom" : "0px",
            "right" : "100px",
            "background" : "yellow"
        },
        "response" : {
            "display" : "none",
            "position" : "fixed",
            "width" : "150px",
            "height" : "200px",
            "bottom" : "0px",
            "right" : "100px",
            "background" : "teal"
        }
    },
    "framing" : {
        "header" : {
            "width" : "150px",
            "height" : "100px",
            "background" : "pink",
            "display" : "none"
        },
        "contentTop" : {
            "width" : "150px",
            "height" : "100px",
            "background" : "purple"
        },
        "contentBottom" : {
            "width" : "150px",
            "height" : "100px",
            "background" : "green"
        },
        "footer" : {
            "width" : "150px",
            "height" : "100px",
            "background" : "blue"
        }
    }
};

function setAttributes(el, attrs) {
  for(var key in attrs) {
    el.setAttribute(key, attrs[key]);
  }
}

var createEl = function(object, target, className) {
    count = 1;
    for(e in object) {
        var newElement = document.createElement("div")
        newElement.id = e;
        setAttributes(newElement, { 
          "class" : className, 
          "data-name" : e, 
          "data-order" :  count
        });

        for(x in object[e]) {
            newElement.style[x] = object[e][x];
        }

        target.appendChild(newElement);
        count++;
    }
}

createEl(json.containers, document.body, "layerFrame");
for(e in json.containers) {
    createEl(json.framing, document.getElementById(e), "framework");
}