js 根据元素生成 css path 路径
作者:互联网
function getCSSPath(node) {
let parts = [];
while (node.parentElement) {
let str = node.tagName.toLowerCase()
if (node.id) {
str += `#${node.id}`;
parts.unshift(str);
break;
}
let siblingsArr = Array.prototype.slice.call(node.parentElement.childNodes);
let ind = siblingsArr.filter((n) => n.attributes).indexOf(node);
parts.unshift(str + `:nth-child(${ind + 1})`);
node = node.parentElement;
}
return parts.join(' > ');
}
标签:node,parentElement,js,parts,let,str,ind,path,css 来源: https://www.cnblogs.com/enncy/p/16157127.html