简体   繁体   English

Google Chrome在开发者工具中复制CSS路径

[英]Google Chrome copy CSS Path in Developer Tools

Google Chrome's Developer Tools shows the CSS path (or a large portion of it) of the selected element at the bottom of the toolbar. Google Chrome的开发人员工具会在工具栏底部显示所选元素的CSS路径(或其中很大一部分)。 In Firebug, you are able to right-click on any selector in the the CSS Path, and grab the CSS Path up to that element. 在Firebug中,您可以右键单击CSS路径中的任何选择器,并获取该路径的CSS路径。 Does Google Chrome have this feature? Google Chrome是否具有此功能? What tools are available if there is no built-in support? 如果没有内置支持,可以使用哪些工具?

Chrome CSS路径

Chrome has updated this option Chrome已更新此选项

In chrome after recent update this option has been changed from 在最近更新后的Chrome中,此选项已更改
(right click on the element in Elements Window) > Copy CSS path
to : 至 :
(right click on the element in Elements Window) > Copy > Copy selector

Chrome doesn't have it, so people have made chrome extensions, bookmarklets, and other tools for Chrome to replicate this functionality. Chrome没有它,因此人们为Chrome提供了chrome扩展,bookmarklet和其他工具来复制此功能。

Possible duplicate: Chrome equivalent of Firefox Firebug CSS select path 可能重复: Chrome等效的Firefox Firebug CSS选择路径

Bookmarklet: http://www.selectorgadget.com/ Bookmarklet: http//www.selectorgadget.com/

Chrome Extension: https://chrome.google.com/webstore/detail/lbghbpofdlcecfbpjgmffnkieenjkboi Chrome扩展程序: https//chrome.google.com/webstore/detail/lbghbpofdlcecfbpjgmffnkieenjkboi

I would still like other people's answers, suggestions, and tips on how to best deal with this in Chrome. 我仍然希望其他人的答案,建议和提示如何在Chrome中最好地处理这个问题。

You can right click on the element in the main source window and "Copy CSS path". 您可以右键单击主源窗口中的元素和“复制CSS路径”。 This has been a life saver when I have to work on pages that I can't re-code. 当我必须处理无法重新编码的页面时,这可以节省生命。

Here is a small (CoffeeScript) snippet that will give CSS path (up to first id element - though you can easily change that by removing the break part): 这是一个小的(CoffeeScript)片段,它将提供CSS路径(直到第一个id元素 - 尽管你可以通过删除break部分轻松地改变它):

getCSSPath = (node)->
  parts = []
  while node.parentElement
    str = node.tagName
    if node.id
      str += "##{node.id}"
      parts.unshift str
      break
    siblingsArr = Array.prototype.slice.call(node.parentElement.childNodes)
    ind = siblingsArr.filter((n)-> n.attributes?).indexOf(node)
    parts.unshift str + ":nth-child(#{ind + 1})"
    node = node.parentElement
  parts.join ' > '

Uses ":nth-child" for every node, so you need to modify it if you'd like to get also class names. 对每个节点使用“:nth-​​child”,因此如果您想获得类名,则需要修改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM