【发布时间】:2015-08-10 07:46:32
【问题描述】:
我有一个使用 forEach 循环复制的 hmtl 模板。我使用模板的 .length 来赋予新的 div 类名,这些类名是唯一的。
问题是它最新的模板包含所有以前的类。
"residentRetainRequest check jqbr_active residentRetainRequest-0 residentRetainRequest-1 residentRetainRequest-2 residentRetainRequest-3"
如何摆脱以前的课程而只保留最新的课程?所以上面的结果是“residentRetainRequest check jqbr_active residentRetainRequest-3”。
背后的原因:
temp.find('.residentRetainRequest').attr 'data-key', 'residentRetainRequest-' + iCnt
是不是我用了另一个函数,用这个来调用它。
【问题讨论】:
-
您确定要为其添加
class的元素对吗?添加类后,通过更改temp.find('.someclass').addClass('newClass').removeClass('someClass');等事件删除classname -
或仅将具有当前计数的类添加到最新元素:
temp.find('.residentRetainRequest').last().addClass("residentRetainRequest-" + iCnt) -
抱歉不够清楚。但我希望所有元素都以
residentRetainRequest- + iCnt作为类名。但只有一个。不是"residentRetainRequest check jqbr_active residentRetainRequest-0 residentRetainRequest-1 residentRetainRequest-2 residentRetainRequest-3" -
所以只需选择最后一个元素(而不是始终使用 .residentRetainRequest 的所有元素)并添加类 residentRetainRequest-i。
-
谢谢,解决了! :)
标签: javascript jquery html loops foreach