等待加载。。。

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(' > ');
    }
posted @ 2022-04-17 20:19  言小溪enncy  阅读(190)  评论(0编辑  收藏  举报