连续的js方法
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getEl
返回scope
变量,它是 DOM 元素的列表,而不是对getElement
的引用。您需要返回this
才能执行诸如new getElement().by(data).style()
之类的操作。然后你可以执行
getEl({id : 'divId', tag : 'span'}).style({display : 'none'})
。要获取
scope
变量,您可以添加如下内容:getEl({id : 'divId', tag : 'span'}).elements()
将返回DOM 元素列表。getEl
returns thescope
variable, which is a list of DOM elements, not a reference togetElement
. You need to returnthis
to be able to do something likenew getElement().by(data).style()
.Then you can do
getEl({id : 'divId', tag : 'span'}).style({display : 'none'})
.To get the
scope
variable, you can add something like this:getEl({id : 'divId', tag : 'span'}).elements()
will return a list of DOM elements.我想通了,感谢 Rocket:)
它适用于:
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 :)
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 instyle()
I'm checking for the length ofthis.scope
. (I couldn't find any way to force the type).Thanks for the help! :)