简体   繁体   English

JavaScript的树无法在Internet Explorer 7上正常工作

[英]Tree with JavaScript not working properly on Internet Explorer 7

I need some help with my JavaScript, as it behaves strangely while I am testing on Internet Explorer 7, but it works fine on Internet Explorer 8,Google Chrome,and firefox. 我需要JavaScript方面的帮助,因为在Internet Explorer 7上进行测试时,它的行为会很奇怪,但在Internet Explorer 8,Google Chrome和firefox上却可以正常工作。 the problem is that when ever i click on the extreme left of P1, P2, Cello, C2, C1 withen the green shaded region, my tree collapse back to Item which should not happen. 问题是,当我单击P1,P2,大提琴,C2,C1的最左端带有绿色阴影区域时,我的树会倒塌到不应发生的项目。 It should be somewhat like that if i click on parker or cell or item, its sub-parts will be closed or open. 如果我单击Parker或单元或项目,它的子部分将关闭或打开,这应该有点像。


 var Objectkeys = function(obj){
        a = []
        for(var prop in obj){
            if (obj.hasOwnProperty(prop)) {
             a.push(prop);
            }
        };
        return a;
    },
    traverseObject = function (obj) {
        var ul = document.createElement("ul"),
            li,span;

        for (var prop in obj) {
            li = document.createElement("li");
            li.appendChild(document.createTextNode(prop));
            li.style.backgroundColor = "green";
            li.onclick = function(e) {
                e = e || window.event;
                if ((e.target || e.srcElement).tagName !== "LI") return;
                var classNames = this.className;
                if (classNames.indexOf("hidden") == -1) {
                    this.className += "hidden";
                } else {
                    this.className = this.className.replace("hidden", "");
                }
                if (!e)
                    e = window.event;
                    if (e.stopPropagation) {
                      e.stopPropagation();
                    }
                else {
                    e.cancelBubble = true;
                }
            }
            if (typeof obj[prop] == "object" && Objectkeys(obj[prop]).length) {

                li.appendChild(traverseObject(obj[prop]));
            } else {
                li.className += "leaf";
            }
            ul.appendChild(li);
            console.log(ul);
        }
        return ul;
    },
    sortedObject = function (obj) {
        document.getElementById("dvList2").innerHTML = "";
        var ul = document.createElement("ul"),
            li,keys = Objectkeys(obj).sort();
        for (prop in keys) {
        var li = document.createElement("li");
            li.appendChild(document.createTextNode(keys[prop]));
            li.onclick = function(e) {
                e = e || window.event;
                if ((e.target || e.srcElement).tagName !== "LI") return;
                var classNames = this.className;
                if (classNames.indexOf("hidden") == -1) {
                    this.className += "hidden";
                } else {
                    this.className = this.className.replace("hidden", "");
                }
                if (!e)
                    e = window.event;
                    if (e.stopPropagation) {
                      e.stopPropagation();
                    }
                else {
                    e.cancelBubble = true;
                }
            }
            if (typeof obj[keys[prop]] == "object" && Objectkeys(obj[keys[prop]]).length) {
                li.appendChild(sortedObject(obj[keys[prop]]));
            } else {
                li.className += "leaf";
            }
            ul.appendChild(li);
            console.log(ul);
        }
        return ul;
    };
window.onload = function () {
    document.getElementById("dvList1").appendChild(traverseObject(dataSource));
    document.getElementById("hlGSL").onclick = function(){document.getElementById("dvList2").appendChild(sortedObject(dataSource));}
}

FIDDLE 小提琴

Your JavaScript works fine, even in IE7 (tested using emulation mode in IE11). 即使在IE7中(使用IE11中的仿真模式进行测试),您的JavaScript也可以正常工作。

The different behaviour you're seeing is because IE7 and below use margin to indent ul elements while all other browsers use padding . 您看到的不同行为是因为IE7及以下版本使用margin缩进ul元素,而所有其他浏览器都使用padding Add following CSS to normalize that: 添加以下CSS进行标准化:

ul {
    margin: 0;
    padding: 0 0 0 40px;
}

You can use the DOM Explorer's element selection tool (hit F12 to open) to see what element you're actually clicking on in order to fix such issues much faster. 您可以使用DOM Explorer的元素选择工具(按F12键打开)来查看您实际单击的元素,以便更快地解决此类问题。

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

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