简体   繁体   English

Internet Explorer上的JavaScript树问题

[英]JavaScript Tree issue on Internet Explorer

I need some help with my JavaScript. 我的JavaScript需要一些帮助。 My JavaScript code is not working on Internet Explorer 8, it gives a blank display,and some error code, on ie console, please suggest me some solution to get this JavaScript code working on Internet Explorer. 我的JavaScript代码在Internet Explorer 8上无法正常工作,显示空白,并在控制台上显示了一些错误代码,请为我提供一些解决方案,以使此JavaScript代码在Internet Explorer上正常工作。 The following is the link for the fiddle: 以下是小提琴的链接:

Fiddle 小提琴


Error Code 错误代码

SCRIPT438: Object doesn't support property or method 'keys' File: treelist.js, Line: 12, Column: 13 SCRIPT438:对象不支持属性或方法“键”文件:treelist.js,行:12,列:13


var dataSource = {
            "Watch": {
                "Titan": {},
                "parent": {
                    "leaf1": {},
                    "leaf2": {}
                },
            }
        },
    traverseObject = function (obj) {
        var ul = document.createElement("ul"),
            li;

        for (var prop in obj) {
            li = document.createElement("li");
            li.appendChild(document.createTextNode(prop));
            li.onclick = function(e) {
                var classNames = e.currentTarget.className;
                if (classNames.indexOf("hidden") == -1) {
                    e.currentTarget.className += "hidden";
                } else {
                    e.currentTarget.className = e.currentTarget.className.replace("hidden", "");
                }
                e.stopPropagation();
            }

            if (typeof obj[prop] == "object" && Object.keys(obj[prop]).length) {
                li.appendChild(traverseObject(obj[prop]));
            } else {
                li.className += "leaf";
            }
            ul.appendChild(li);
            console.log(ul);
        }
        return ul;
    };


window.onload = function () {
    document.getElementById("dvList1").appendChild(traverseObject(dataSource));
}

Your code uses Object.keys() , which is not supported in Internet Explorer 8 (support was introduced in IE9). 您的代码使用Object.keys() ,Internet Explorer 8不支持此功能(在IE9中引入了支持)。 MDN has more information , including a polyfill you can use to fix your problem. MDN有更多信息 ,包括可用于解决问题的polyfill。 Simply paste it into the relevant Javascript file. 只需将其粘贴到相关的Javascript文件中即可。

As Zach L pointed out, your code also uses e.stopPropogation() . 正如Zach L所指出的那样,您的代码还使用e.stopPropogation() This is also unsupported by IE8 (again, it was introduced in IE9). IE8也不支持此功能(同样,它是IE9中引入的)。 MDN has more information , and Jonathan T. Neal has a polyfill you can use to work around the problem (it's the addEventListener code at the end of the article). MDN提供了更多信息Jonathan T. Neal提供了一个polyfill,您可以使用它来解决该问题(这是本文结尾处的addEventListener代码)。 Again, just paste it in. 同样,只需将其粘贴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM