我尝试使用jquery(jquery-2.1.1)和构造函数来构建html。但是当我在for循环中尝试这种方法时,循环无法停止。谁能告诉我为什么会发生?为什么这个循环无法停止?
下面是代码。非常感谢。
function tag() {
this.addClass = function(...theArgs) {
for (index in theArgs) {
this.$html.addClass(theArgs[index]);
}
}
this.setAttr = function(attr, value) {
this.$html.attr(attr, value);
}
this.append = function(...theArgs) {
for (index in theArgs) {
this.$html.append(theArgs[index]);
}
}
this.find = function(value) {
return this.$html.find(value);
}
this.empty = function() {
this.$html.empty();
}
this.remove = function(value) {
this.find(value).remove();
}
this.clone = function() {
return jQuery.extend(true, {}, this);
}
this.show = function() {
return this.$html[0];
}
}
function label(text) {
tag.call(this);
this.$html = $("<label></label>");
this.append(text);
}
for(var index = 0; index < 2; index++) {
var fieldLabel = new label(1);
console.log(index);
}
在'.append()'中有一个undeclard'index',它们有冲突:'for(index中的Args){'。一定要声明你的变量:'for(varArgs在Args中){' – 2017-04-06 14:21:40
...同样在'addClass()'中。使用类似ESLint的linter来查找这些常见错误。它会为你节省很多很多时间。 – 2017-04-06 14:22:48
...并且在数组中使用'for-in'在JS中并不是一个好主意。既然你使用'...'语法,你必须使用ES6的东西,所以我建议使用'for-for'循环。 – 2017-04-06 14:25:27