【发布时间】:2016-02-19 12:19:27
【问题描述】:
我正在努力编写一个 jQuery .each() 函数来循环遍历我从 Raphael 对象构建的数组。我可以用传统的 JS for-loop 做到这一点:
for (var i = 0; i < regions.length; i++) {
regions[i].node.setAttribute('fill', '#113381');
}
但是当我尝试这个时:
$.each(regions, function (index) {
$(this).node.setAttribute('fill', '#113381');
});
我收到一个错误:TypeError: undefined is not an object (evalating '$(this).node.setAttribute')
一个典型的数组项如下所示:
var someName = rsr.path("M295.499,153.782l-0.77,4.605 l-5.786,1.016l-2.313-2.887l-1.233-3.278l-3.014-0.469l-3.39,1.013l-6.479,6.557l-5.575-0.332l-0.982-0.059l-3.936-2.262 l0.455-0.856l3.019-5.701l-1.623-1.325l2.408-4.141l11.51-0.261l5.212-0.119l-0.54,4.988l2.008,1.326l6.172-0.075L295.499,153.782z M274.037,160.428l2.193,1.402l5.325-5.342l-1.563-0.904L274.037,160.428z").attr({parent: 'someName',fill: '#CCCCCC',stroke: '#FFFFFF',"stroke-width": '0.54',"stroke-miterlimit": '10','stroke-opacity': '1'}).data('id', 'someName');
someName.attr({'id': 'someName','name': 'someName'});
regions.push(someName);
我已经声明了数组区域,它绘制了我的 Rapahel 对象。如果我能得到任何帮助,我将不胜感激。非常感谢。
【问题讨论】:
-
尝试不将
this包装在 jQuery 函数this.node中。 (它抱怨的是 jQuery 中没有node方法)。或:$.each(regions, function (el) { el.node.setAttribute('fill', '#113381');}); -
@andy 谢谢,但这不起作用。我仍然得到一个错误 - undefined is not an object
-
好的,这似乎可行:
$.each(regions, function (index) { this.node.setAttribute('fill', '#003300'); });
标签: javascript jquery arrays raphael