我正在尝试使用 Typescript 创建一个像 JQuery 这样的库。我试图走捷径,这样我就不必一遍又一遍地重复基本相同的事情。情况是这样的:
if (property=="color"){
if(this.element_type=="class"){
for(var i=0;i<this.element_focus.length;i++){
this.element_focus[i].style.color = value;
}
}
if(this.element_type=="id"){
this.element_focus.style.color = value;
}
}
我想要一种以动态方式添加 .color 的方法。例如, var What_style = .color 这样我就可以写: this.element_focus.style.what_style=value;有没有办法做类似的事情?
最佳答案
使用 bracket notation :
this.element_focus.style[property] = value;
关于javascript - 我怎样才能动态地给出应该对元素执行什么操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210354/