Skip to content

Commit 4ada504

Browse files
author
dengxinxin
committed
增加从节点位置获取索引的分支方案
1 parent a4f9536 commit 4ada504

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Crumb.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ define(
9595

9696
function click(e) {
9797
var node = e.target;
98+
var children = lib.getChildren(this.main);
9899
while (node !== this.main) {
99100
if (this.helper.isPart(node, 'node')) {
100-
var index = e.target.getAttribute('data-index');
101+
var index = node.hasAttribute('data-index') ? node.getAttribute('data-index')
102+
: getPathIndex(children, node);
101103
var event = this.fire('click', { item: this.path[index] });
102104
event.isDefaultPrevented() && e.preventDefault();
103105
return;
@@ -107,6 +109,15 @@ define(
107109
}
108110
}
109111

112+
function getPathIndex(children, node) {
113+
for (var i = children.length - 1; i > -1; i -= 2) {
114+
if (children[i] === node) {
115+
// separator 的插入使得索引要除个2
116+
return i / 2;
117+
}
118+
}
119+
}
120+
110121
/**
111122
* 无链接的文字节点的内容HTML模板
112123
*

0 commit comments

Comments
 (0)