我有这个函数来获取一个cssPath:
var cssPath = function (el) {
var path = [];
while (
(el.nodeName.toLowerCase() != 'html') &&
(el = el.parentNode) &&
path.unshift(el.nodeName.toLowerCase() +
(el.id ? '#' + el.id : '') +
(el.className ? '.' + el.className.replace(/\s+/g, ".") : ''))
);
return path.join(" > ");
}
console.log(cssPath(document.getElementsByTagName('a')[123]));
Run Code Online (Sandbox Code Playgroud)
但我有这样的事情:
html > body > div#div-id > div.site > div.clearfix > ul.choices > li
但要完全正确,它应该是这样的:
html > body > div#div-id > div.site:nth-child(1) > div.clearfix > ul.choices > li:nth-child(5)
有人有任何想法只是在javascript中实现它?