连续的js方法

发布于 2024-10-31 03:19:09 字数 1241 浏览 0 评论 0原文

我有一个 JS 函数,它根据参数选择一些元素,

function getElement() {
    var scope = document;

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    scope = scope.getElementById(data.id);
            if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
        }       
        return scope;
    }
}

function getEl(data) { return new getElement().by(data); }

这被称为 getEl(id : 'divId', tag : 'span') ,它将选择 div 'divId' 中的所有范围。

现在,我想创建另一个函数(在函数 Element 内),称为 style ,它将更改所有先前选择的跨度上的 CSS。

function getElement() {
    var scope = document;

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    scope = scope.getElementById(data.id);
            if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
        }       
        return scope;
    }

    this.style = function(data) {
        console.log(data);
    }
}

我希望能够做类似的事情 getEl({id : 'divId', tag : 'span').style({display : 'none'})

似乎不起作用,我收到 getEl({id: "divId", tag: "span"}).style is undefined 错误。

ps:这仅用于学习目的,请勿推荐 jQuery 或其他此类框架! :)

亲切的问候!

I have a JS function that selects some elements based on the parameters

function getElement() {
    var scope = document;

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    scope = scope.getElementById(data.id);
            if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
        }       
        return scope;
    }
}

function getEl(data) { return new getElement().by(data); }

This is called like getEl(id : 'divId', tag : 'span') and it would select all spans in the div 'divId'.

Now, I would like to make another function (inside function Element), called style that would change the CSS on all previously selected spans.

Something like

function getElement() {
    var scope = document;

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    scope = scope.getElementById(data.id);
            if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
        }       
        return scope;
    }

    this.style = function(data) {
        console.log(data);
    }
}

I would like to be able to do something like getEl({id : 'divId', tag : 'span').style({display : 'none'})

But this doesn't seem to work and I receive a getEl({id: "divId", tag: "span"}).style is undefined error.

ps: this is for learning purposes only, please do not suggest jQuery or other such frameworks! :)

Kind regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-11-07 03:19:09

getEl 返回 scope 变量,它是 DOM 元素的列表,而不是对 getElement 的引用。您需要返回 this 才能执行诸如 new getElement().by(data).style() 之类的操作。

this.by = function(data) {
    for (key in data) {
        if (key == 'id')    scope = scope.getElementById(data.id);
        if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
    }       
    return this;
}

然后你可以执行getEl({id : 'divId', tag : 'span'}).style({display : 'none'})

要获取 scope 变量,您可以添加如下内容:

this.elements = function(){
    return scope;
}

getEl({id : 'divId', tag : 'span'}).elements() 将返回DOM 元素列表。

getEl returns the scope variable, which is a list of DOM elements, not a reference to getElement. You need to return this to be able to do something like new getElement().by(data).style().

this.by = function(data) {
    for (key in data) {
        if (key == 'id')    scope = scope.getElementById(data.id);
        if (key == 'tag')   scope = scope.getElementsByTagName(data.tag);
    }       
    return this;
}

Then you can do getEl({id : 'divId', tag : 'span'}).style({display : 'none'}).

To get the scope variable, you can add something like this:

this.elements = function(){
    return scope;
}

getEl({id : 'divId', tag : 'span'}).elements() will return a list of DOM elements.

蒲公英的约定 2024-11-07 03:19:09

我想通了,感谢 Rocket:)

function getElement() {
    this.scope = document;

    this.get = function() {
        return this.scope;
    }

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    this.scope = this.scope.getElementById(data.id);
            if (key == 'tag')   this.scope = this.scope.getElementsByTagName(data.tag);

        }
        return this;
    }

    this.style = function(data) {
        console.log(typeof(this.scope));
        for (key in data) {
            if (this.scope.length) {
                for (i = 0; i < this.scope.length; i++) {
                    this.scope[i].style[key] = data[key];
                }
            }
            else {
                this.scope.style[key] = data[key];
            }
        }
        return this;
    }
}
function getEl(data) { return new getElement().by(data); }

它适用于:
getEl({id : "tara_content", tag : "div"}).style({display : "none"});
getEl({id: "tara_content"}).style({display: "none"});
getEl({tag : "div"}).style({display : "none"});

正如 Mathias Bynens 注意到 by() 可以返回一个元素数组或只是一个元素,这就是为什么在 style() 中我要检查 this.scope 的长度。 (我找不到任何方法来强制该类型)。

感谢您的帮助! :)

I figured it out, thanks to Rocket :)

function getElement() {
    this.scope = document;

    this.get = function() {
        return this.scope;
    }

    this.by = function(data) {
        for (key in data) {
            if (key == 'id')    this.scope = this.scope.getElementById(data.id);
            if (key == 'tag')   this.scope = this.scope.getElementsByTagName(data.tag);

        }
        return this;
    }

    this.style = function(data) {
        console.log(typeof(this.scope));
        for (key in data) {
            if (this.scope.length) {
                for (i = 0; i < this.scope.length; i++) {
                    this.scope[i].style[key] = data[key];
                }
            }
            else {
                this.scope.style[key] = data[key];
            }
        }
        return this;
    }
}
function getEl(data) { return new getElement().by(data); }

It works with:
getEl({id : "tara_content", tag : "div"}).style({display : "none"});
getEl({id : "tara_content"}).style({display : "none"});
getEl({tag : "div"}).style({display : "none"});

As Mathias Bynens noticed by() can return an array of elements or just one element, this is why in style() I'm checking for the length of this.scope. (I couldn't find any way to force the type).

Thanks for the help! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文