JavaScript对象无法在IE中使用

las*_*off 0 javascript jquery internet-explorer

下面的代码可在我尝试过的所有浏览器上使用,但不适用于IE。的jsonObj结尾为所有空值。(浏览器测试了Mac safari,chrome,firefox,PC FireFox,Opera,chrome)IE是唯一失败的。有人可以看到我的问题吗?

IE 10版

function Save() {
    var path = document.location.pathname;

    var Checked = "{";
    jsonObj = [];

    $('.questionsOnPage').each(function () {

        item = {}
        var id = this.id;

        jQuery(this).children(".questionCheckBox").each(function () {
            item ["id"] = this.id;
            item ["selected"] = this.checked;
        });

        jQuery(this).children(".question").each(function () {
            item ["question"] = this.innerHTML;
        });

        answers = {}

        jQuery(this).children(".answer").each(function () {
            answer = {};
            answer ["selector"] = $(this).attr("data-selector");
            answer ["answerText"] = $(this).attr("data-answerText");
            answer ["correct"] = $(this).attr("data-correct");
            answers [$(this).attr("data-selector")] = answer;
        });

        item["answers"] =  answers;

        jsonObj.push(item);
    });
Run Code Online (Sandbox Code Playgroud)

Twi*_*Sun 5

正如Deryck所建议的那样,我将其放在答案中。

您问题的解决方案:在item = {}之前添加Var

不同的浏览器供应商在JavaScript引擎上具有不同的实现。据我观察,当您将某些内容推送到IE中的数组中时,您推送的是对象的引用,而不是克隆的副本(不确定是否为真)。因此,在推送后修改项目将导致更改先前推送的对象。添加var将确保您在每个迭代步骤中都会获得一个新副本。