题解 | #包围#
包围
http://www.nowcoder.com/practice/fcf91b13e4204fb897ea408986ea1b57
function bind() { window.addEventListener('click', (e) => { if (e.target.tagName !== 'TD') return handler(e.target) }) function findIndex(tag, nn){ let n = -1; let out = tag.parentNode for(let i=0, imax=out.children.length; i<imax; i++) { if (out.children[i] == tag) { n = i } } return n } function findEle(tag, n) { let out = tag.parentNode let pos = findIndex(tag) return out.children[pos+n] } function renderEle(tag, isMain, n) { if (!tag) return isMain ? tag.className = 'current' : tag.className = 'wrap'; let prev = tag.parentNode.children[n-1] let next = tag.parentNode.children[n+1] next? next.className = 'wrap' : false; prev? prev.className = 'wrap' : false; } function handler(tag){ let ids = document.getElementsByTagName('td') for(let i=0, imax=ids.length; i<imax; i++) { ids[i].className = '' } n = findIndex(tag, 0) upTag = findEle(tag.parentNode, -1); downTag = findEle(tag.parentNode,1) upTag ? renderEle(upTag.children[n], false, n): false downTag ? renderEle(downTag.children[n], false, n): false renderEle(tag, true, n) } }