如何为 DOM 元素生成唯一的 css 选择器?
我需要为元素生成唯一的 css 选择器。
特别是,我有 onclick 事件处理程序,它应该记住哪个目标元素 被点击并将此信息发送到我的服务器。有没有办法在不修改 DOM 的情况下做到这一点?
PS 我的 javascript 代码应该在不同的平台上运行
第三方网站,所以我不能对 html 做出任何假设。
I need to generate unique css selector for elements.
Particularly, I have onclick event handler that should remember what target element
was clicked and send this info to my server. Is there a way to do it without doing DOM modifications?
P.S. my javascript code supposed to be run on different
3-rd party websites so I can't make any assumptions about html.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该函数创建了一个很长但非常实用的独特选择器,工作速度很快。
结果示例:
以下代码创建了一个稍微漂亮且简短的选择器
结果示例:
This function creates a long, but quite practical unique selector, works quickly.
Example result:
The following code creates a slightly more beautiful and short selector
Example result:
检查此 CSS 选择器生成器库 @medv/finder
生成的选择器示例:
Check this CSS selector generator library @medv/finder
Example of generated selector:
是的,你可以这样做。但有一些注意事项。为了能够保证选择器是唯一的,您需要使用并未得到普遍支持的
:nth-child()
。如果您想将这些 CSS 选择器放入 CSS 文件中,则它不会在所有浏览器中工作。我会这样做:
然后将其添加到您想要建模的每个元素的点击处理程序中。我将让您实现
getChildNumber()
。编辑:刚刚看到您关于它是第 3 方代码的评论...因此您可以添加一个
event
参数,将this
的所有实例替换为event.target
然后将该函数附加到window
的点击事件(如果这样更简单)。Yes, you could do this. But with a few caveats. In order to be able to guarantee that selectors are unique, you'd need to use
:nth-child()
which isn't universally supported. If you're then wanting to put these CSS selectors into CSS files, it won't work in all browsers.I'd do it with something like this:
Then add that to your click handler for each element you want to model. I'll leave you to implement
getChildNumber()
.Edit: Just seen your comment about it being 3rd party code... so you could add an
event
argument, replace all instances ofthis
withevent.target
and then just attach the function towindow
's click event if that's easier.Firefox 开发者控制台使用以下启发式方法来查找元素的唯一选择器。该算法返回找到的第一个合适的选择器。 (来源 )
如果元素有唯一的
id
属性,返回 id 选择器#
。如果标签是
html
、head
或body
,则返回类型选择器
.对于元素的每个类:
如果该类对于该元素是唯一的,则返回类选择器
.
。如果标签/类组合是唯一的,则返回选择器
.
。计算元素在其父元素的子列表中的索引。如果选择器
.:nth-child()
是唯一的,则返回该选择器。让
index
为其父元素的子列表中的索引。如果父节点是根节点,则返回选择器
:nth-child()
。否则,递归查找父节点的唯一选择器并返回
> <元素名称>:nth-child(<索引>)
。The Firefox developer console uses the following heuristics to find a unique selector for an element. The algorithm returns the first suitable selector found. (Source)
If the element has a unique
id
attribute, return the id selector#<idname>
.If the tag is
html
,head
, orbody
, return the type selector<elementname>
.For each class of the element:
If the class is unique to the element, return the class selector
.<classname>
.If the tag/class combination is unique, return the selector
<elementname>.<classname>
.Compute the element's index in its parent's child list. If the selector
<elementname>.<classname>:nth-child(<index>)
is unique, return that.Let
index
be the element's index in its parent's child list.If the parent node is the root node, return the selector
<elementname>:nth-child(<index>)
.Otherwise, recursively find a unique selector for the parent node and return
<parentselector> > <elementname>:nth-child(<index>)
.您可能可以从节点遍历 DOM 树回到 body 元素以生成选择器。
Firebug 有一个功能,可以使用 XPath 和 CSS 选择器。
请参阅此答案
You could probably traverse the DOM tree from the node back to the body element to generate a selector.
Firebug has a feature for this, both using XPath and CSS selectors.
See this answer
假设您有一个链接列表,为了简单起见:您可以简单地传递
js 所有元素集合中触发元素的索引(jQuery 1.7+,我使用
.on()
否则使用bind()
) 函数可以这样,如果您单击第三个元素,传递的索引值将为 2。(从 0 开始的索引);
当然,只要代码(DOM)不改变,这就是有效的。稍后您可以使用该索引为该元素创建 css 规则,例如使用
:nth-child
否则,如果每个元素都有不同的属性(如 id),您可以将该属性
示例传递给JsFiddle : http://jsfiddle.net/t7J8T/
let say you have a list of links for the sake of simplicity: you can simply pass the index of the triggering element in the collection of all elements
the js (jQuery 1.7+, I used
.on()
otherwise usebind()
) function can beso that if you click on third element index value passed will be 2. (0-based index);
of course this is valid as long as the code (the DOM) doesn't change. Later you can use that index to create a css rule to that element e.g. using
:nth-child
Otherwise if each one of your elements have a different attribute (like an id) you can pass that attribute
example on JsFiddle : http://jsfiddle.net/t7J8T/
你可以试试这个。不使用jquery。
you can try this one. without using jquery.