网站技术
当前目录:首页 >> 新闻中心 >> 网站技术

【原创】无限级下拉菜单-JS

发布人:卓伟软件
发布日期:2014-05-07 00:51:15
浏览次数:1463

/*

作者:jacky

QQ:7092734

网址:http://www.d369.net

E-mail:jacky@d369.net

*/

var SelectClass = function () {

    this.Name = ""; this.type = 0; this.Source = new Object(); this.FirstOption = [["==请选择==", "0"]];
    this.SelectFirst = true; this.Select = ""; this.Init = true; this.isCache = false;
    var self = this, arg = arguments, __f = true;
    this.Fun = function (e) { return; }
    this.Run = function () {
        if (!this.Init) return;
        this.Select = this.$(this.Name).value;
        if (!this.$(this.Name)) {
            var _Input = this.$C("input"); _Input.type = "hidden"; _Input.name = this.Name; _Input.id = this.Name;
            document.body.appendChild(_Input);
        }
        this.$(this.Name).value = this.Select;
        if (this.Select != "") this.FormatData();
        if (typeof this.Source == "object")
            if (this.Source.hasChildNodes) {
                this.CreateSelect(this.Source);
                if (this.Select == "" || this.Select == "0") {
                    var __Select = this.$(this.Name).parentNode.getElementsByTagName("select");
                    if (this.SelectFirst)
                        this.$(this.Name).value = this.value(__Select[__Select.length - 1]);
                    else
                        this.$(this.Name).value = this.value(__Select[__Select.length - 1].options[1]);
                } else { }
            } else {
                var _Span = this.$C("span");
                _Span.innerHTML = "<span style=\"color:#FF0000;\">数据源为空.</span>";
                this.$(this.Name).parentNode.appendChild(_Span);
            }
        else {
            var _Spans = this.$C("span");
            _Spans.innerHTML = "<span style=\"color:#FF0000;\">数据源出错.</span>";
            this.$(this.Name).parentNode.appendChild(_Spans);
        }
        if (this.$(this.Name).value == "" || this.$(this.Name).value == "undefined") this.$(this.Name).value = "0";
    }
    this.CreateSelect = function (node) {
        if (!node.hasChildNodes || node.childNodes.length < 1 || node.getAttribute("ChildCount") == "0") { this._SelectFirstName(); return; }
        var Select = this.$C("select");
        Select.id = Select.name = "select_" + (new Date().getTime());
        var _f = false, _s = 0;
        if (typeof this.FirstOption == "string") this.FirstOption = [];
        if (typeof this.FirstOption == "object" && this.FirstOption.length > 0)
            Select.options.add(this.CreateOption("==请选择==", "0", false));
        for (var i = 0; i < node.childNodes.length; i++)
            if (node.childNodes[i].nodeType == 1) {
                if (node.childNodes[i].getAttribute("s") == null) { _f = false; } else { _f = true; _s = i; }
                Select.options.add(this.CreateOption(node.childNodes[i].getAttribute("Name"), node.childNodes[i].getAttribute("ID"), node.childNodes[i].getAttribute("aID"), _f));
            }
        Select.onchange = function () {
            __f = false; self.SelectFirst = true;
            self._RemoveSelect(this);
            self._SelectNode(self.Source, this.value);
            var __Select = this.parentNode.getElementsByTagName("select");
            self.$(self.Name).value = self.value(__Select[__Select.length - 1]);
            self.Fun(this);
        };
        this.$(this.Name).parentNode.insertBefore(Select, this.$(this.Name));
        if (__f && !this.SelectFirst && (this.Select.Trim(",") == "" || this.Select.Trim(",") == "0")) { this._SelectFirstName(); return; }

        try {
            this.Select = (function (ID, sValue) {
                return sValue.replace("," + ID, "");
            })(node.childNodes[_s].getAttribute("ID"), this.Select);
            if (this.Select == ",") { this.$(this.Name).value = node.childNodes[_s].getAttribute("ID"); return; }
        } catch (e) { }
        if (_s > 0) {
            this.CreateSelect(node.childNodes[_s]);
        } else {
            this.CreateSelect(node.childNodes[node.childNodes[0].nodeType == 1 ? 0 : 1]);
            try {
                if (this.SelectFirst)
                    if (this.FirstOption.length == 0)
                        Select.options[0].selected = true;
                    else
                        Select.options[1].selected = true;
                else {
                    var _f = false;
                    for (var i = 0; i < Select.options.length; i++) {
                        if (Select.options[i].selected == true) {
                            _f = true; break;
                        }
                    }
                    if (!_f) Select.options[0].selected = true;
                }
            } catch (e) { Select.parentNode.removeChild(Select); }
        }
    }
    this.CreateOption = function (t, v, l, f) {
        var OptionSub = new Option();
        OptionSub.text = t; OptionSub.value = v; OptionSub.aID = l;
        if (f) OptionSub.selected = f;
        return OptionSub;
    }
    this._SelectFirstName = function () {
        var _Select = this.$(this.Name).parentNode.getElementsByTagName("select");
        var self = this, _f = true, _FirstOption = [], _FirstLength = this.FirstOption.length;
        if (this.FirstOption.length == 0) _f = false;
        for (var i = 0; i < _Select.length; i++) {
            if (_f) {
                if (i >= _FirstLength)
                    _FirstOption = this.FirstOption[0];
                else
                    _FirstOption = this.FirstOption[i];
                _Select[i].options[0].text = _FirstOption[0];
                _Select[i].options[0].value = _FirstOption[1];
            }
            _Select[i].onchange = function () {
                __f = false; self.SelectFirst = true;
                self._RemoveSelect(this);
                self._SelectNode(self.Source, this.value);
                var __Select = this.parentNode.getElementsByTagName("select");
                self.$(self.Name).value = self.value(__Select[__Select.length - 1]);
                if (self.FirstOption.length != 0 && this.selectedIndex == 0)
                    if (_Select.length > 1)
                        self.$(self.Name).value = self.value(__Select[__Select.length - 2]);
                self.Fun(this);
            };
        }
    }
    this._SelectNode = function (node, s) {
        for (var i = 0; i < node.childNodes.length; i++)
            if (node.childNodes[i].nodeType == 1) {
                if (node.childNodes[i].getAttribute("ID") == s)
                    this.CreateSelect(node.childNodes[i]);
                else
                    if (node.childNodes[i].hasChildNodes) this._SelectNode(node.childNodes[i], s);
            }
    }
    this._RemoveSelect = function (o) {
        var _Select = this.$(this.Name).parentNode.getElementsByTagName("select");
        var _f = false;
        for (var i = 0; i < _Select.length; i++) {
            if (_Select[i] == o) { _f = true; continue; }
            if (_f) { _Select[i].parentNode.removeChild(_Select[i]); i--; }
        }
    }
    this.FormatData = function () {
        if (typeof this.Source != "object" || this.Select.indexOf(",") == 0) return;
        this.$(this.Name).value = this.Select.substring(this.Select.lastIndexOf(",") + 1, this.Select.length);
        this.Select = "," + this.Select + ",";
        this._FormatData(this.Source);
    }
    this._FormatData = function (node) {
        if (!node.hasChildNodes) return;
        for (var i = 0; i < node.childNodes.length; i++)
            if (node.childNodes[i].nodeType == 1) {
                if (this.Select.indexOf(("," + this.nodeValue(node.childNodes[i]) + ",").toString()) != -1) node.childNodes[i].setAttribute("s", "1");
                if (node.childNodes[i].hasChildNodes) this._FormatData(node.childNodes[i]);
            }
    }
    this.clearFormat = function (node) {
        if (!node.hasChildNodes) return;
        for (var i = 0; i < node.childNodes.length; i++)
            if (node.childNodes[i].nodeType == 1) {
                if (node.childNodes[i].getAttribute("s") == "1") node.childNodes[i].setAttribute("s", "0");
                if (node.childNodes[i].hasChildNodes) this.clearFormat(node.childNodes[i]);
            }
    }
    this.value = function (o) {
        try {
            switch (this.type) {
                case 0: return o.options[o.selectedIndex].value; break;
                case 1: return o.options[o.selectedIndex].text; break;
                case 2: return o.options[o.selectedIndex].aID; break;
                default: return o.value; break;
            }
        } catch (e) { o.value; }
    }
    this.nodeValue = function (o) {
        switch (this.type) {
            case 0: return o.getAttribute("ID"); break;
            case 1: return o.getAttribute("Name"); break;
            case 2: return o.getAttribute("aID"); break;
            default: return o.getAttribute("ID"); break;
        }
    }
    this.$ = function (e) { return document.getElementById(e); }
    this.$C = function (e) { return document.createElement(e); }
    this.loadXML = function (Path) {
        var Dom = null;
        try {
            if (window.ActiveXObject)
                Dom = new ActiveXObject("Microsoft.XMLDOM");
            else
                Dom = document.implementation.createDocument("", "", null);
            Dom.async = false;
            Dom.load(Path);
        } catch (e) {
            var xmlhttp = new window.XMLHttpRequest();
            xmlhttp.open("GET", Path, false);
            xmlhttp.send(null);
            Dom = xmlhttp.responseXML;
        }
        return Dom;
    }
    this.addEvent = function (a, b, c, d) {
        if (a.attachEvent) a.attachEvent(b[0], c);
        else a.addEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
        return c;
    }
    this.init = (function () {
        if (arg.length == 0) self.Init = false;
        if (arg.length == 1 && typeof arg == "object") {
            self.Name = arg[0].ID; self.type = typeof arg[0].type == "undefined" ? 0 : arg[0].type; self.Source = arg[0].Data;
            self.FirstOption = typeof arg[0].firstOption == "undefined" ? [] : arg[0].firstOption; self.SelectFirst = typeof arg[0].selectFirst == "undefined" ? true : arg[0].selectFirst;
            self.Fun = typeof arg[0].fun == "undefined" ? function (e) { return; } : arg[0].fun;
            var file = typeof arg[0].file == "undefined" ? "" : arg[0].file; self.isCache = arg[0].isCache || false;
            var xml = file == "" ? null : self.loadXML(file + (self.isCache ? "" : ((file.indexOf("?") == -1 ? "?" : "&") + "r=" + Math.random())));
            self.Source = self.Source == null ? (xml == null ? null : xml.documentElement) : self.Source;
        } else if (arg.length == 7) {
            self.Name = arg[0]; self.type = arg[6]; self.Source = arg[1]; self.FirstOption = arg[2]; self.SelectFirst = arg[4]; self.Fun = arg[3];
        }
        self.Init = true; self.addEvent(window, ["onload"], function () { self.Run(); });
    })();
}
String.prototype.Trim = function (s) { return this.TrimStart(s).TrimEnd(s); } //清空开始与结尾的指定字符
String.prototype.TrimStart = function (s) {//清空开始的指定字符
    if (!s) s = "\\s+";
    var trimStartStr = new RegExp("^(" + s + ")+", "g");
    return this.replace(trimStartStr, "");
}
String.prototype.TrimEnd = function (s) {//清空结尾的指定字符
    if (!s) s = "\\s+";
    var trimEndStr = new RegExp("(" + s + ")+$", "g");
    return this.replace(trimEndStr, "");
}

/*

调用方法

new SelectClass({ ID: "hfID", file: "my.xml", firstOption: [["==请选择==", "0"]], selectFirst: true });
*/
Copyright © 2010 - 2015 Zhuovi. All Rights Reserved
联系电话:15837199978  E-Mail:service@zhuovi.com  在线QQ:点击这里给我发消息 点击这里给我发消息  备案号:豫ICP备14022191号
郑州网站建设 | 河南网站建设 | 郑州软件 | 河南软件 | 办公软件 | 移动短信 | 注册域名 | 虚拟空间,网站空间 | 群发短信 | 网站优化 | 网站推广 | 优化技术 | 郑州作网站 | 河南作网站
在线咨询 返回顶部