loop
方法即循环遍历Path
中的元素,对每个元素执行给定的回调函数。
Path.loop(callback[, context])
参数:
callback
:回调函数,接收两个参数,分别是当前遍历到的元素和当前元素的索引值。context
:可选参数,指定回调函数中的this
值,默认为Path
本身。const myPath = new Path('#my-path');
myPath.loop(function(element, index) {
console.log(`第 ${index+1} 个元素的id值是 ${element.id}`);
});
输出结果为:
第 1 个元素的id值是 element-1
第 2 个元素的id值是 element-2
第 3 个元素的id值是 element-3
Path
对象中。Path
对象中的顺序相同。