如何在javascript document.createElement上应用链接方法

时间:2016-01-27 01:03:11

标签: javascript chaining

我想在使用javascript' document.createElement()创建的元素上创建一个jQuery类型链接。以下代码生成错误"无法调用方法' appendChild'未定义"每当我试图运行我的"追加"我的函数定义的父对象上的方法。任何帮助或建议都表示赞赏。

 this.el = (function () {
    function _el() {
        var self = this,
        ele;

        this.add = function (tag) {
            ele = document.createElement(tag);
            return this;
        },

        this.byId = function (id) {
            ele = document.getElementById(id);
            return this;
        },

        this.byClass = function (cl) {
            ele = document.getElementsByClassName(cl);
            return this;
        },

        this.id = function (name) {
            ele.id = name;
            return this;
        },

        this.cl = function (name) {
            ele.className = name;
            return this;
        },

        this.css = function (style) {
            _this.setCSS(ele, style);
            return this;
        },

        this.html = function (str) {
            ele.innerHTML = str;
            return this;
        },

        this.append = function (parent) {
            if (parent.nodeType === 1) {
                parent.appendChild(ele);
            }
            console.log(ele);
            console.log(ele.nodeType);

            return this;
        };
        return this;
    }
    return new _el();
}());

这就是我在代码中使用该函数的方法。第一次使用有效,第二次使用没有。它与我的函数返回的对象类型有关,但我不确定如何纠正。

var dialog = hlp.el.add("div").cl("alphaDialog").append(document.body);

var top = hlp.el.add("div").append(dialog);

1 个答案:

答案 0 :(得分:0)

this.append函数返回保存this js对象的_ele对象。我们必须返回我们的HTML元素ele。在this.append我们return ele;

 this.el = (function () {
    function _el() {
        var self = this,
        ele;

        this.add = function (tag) {
            ele = document.createElement(tag);
            return this;
        },

        this.byId = function (id) {
            ele = document.getElementById(id);
            return this;
        },

        this.byClass = function (cl) {
            ele = document.getElementsByClassName(cl);
            return this;
        },

        this.id = function (name) {
            ele.id = name;
            return this;
        },

        this.cl = function (name) {
            ele.className = name;
            return this;
        },

        this.css = function (style) {
            _this.setCSS(ele, style);
            return this;
        },

        this.html = function (str) {
            ele.innerHTML = str;
            return this;
        },

        this.append = function (parent) {
            if (parent.nodeType === 1) {
                parent.appendChild(ele);
            }
            console.log(ele);
            console.log(ele.nodeType);

            //return this; // this holds javascript object, not element

            return ele;   // return our ele variable which holds the element
                          // this.append() is the end of the chain
        };
        return this;
    }
    return new _el();
}());