Zac*_*aro 7 html dom google-chrome
当您选择Inspect Element
Chrome时,在Developer's面板的底部会显示该元素的路径.
在这种情况下,它是body > div#divsinglecolumnwidth.singlecolumnwidth > div#productdescription>h2
.
有没有办法复制那条"路径"?
小智 8
当您在 DOM 检查器中选择一个元素时,该元素在控制台中将变为 $0:
“在控制台中使用 $0 来引用此元素”
因此,您只需转到控制台并粘贴以下内容即可:
crumb = function(node) {
var idOrClass = (node.id && "#"+node.id) || (""+node.classList && (" "+node.classList).replace(/ /g, "."));
return node.tagName.toLowerCase() + idOrClass;};
crumbPath = function(node) {return node.parentNode ? crumbPath(node.parentNode).concat(crumb(node)) : [];};
crumbPath($0);
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
["html", "body.question-page.new-topbar", "div.container._full.", "div#content", "div", "div.inner-content.clearfix", "div#mainbar", "div#question", "div.post-layout", "div.postcell.post-layout--right", "div.post-text", "p"]
Run Code Online (Sandbox Code Playgroud)
只需右键单击 DOM 节点,然后选择Copy xPath
选项。
您将得到一个如下所示的标准 xPath://*[@id="copyright"]/a[1]