从元素获取 CSS 选择表达式
我正在寻找通过传递元素来获取元素的选择表达式的最佳方法,即:我想要 sizzle/slick 的相反...我想传递我单击的元素并获得唯一的选择它的表达式(例如:'table[0] tr td[5]') 有一个图书馆可以做到这一点吗?或者我必须通过遍历 dom 自己构建它?
提前致谢 。
I'm looking for the best way to get a selection expression for an element by passing the element , i.e. : i want the reverse of sizzle/slick ... i want to pass an element i've clicked on and get a unique selection expression for it (e.g. : 'table[0] tr td[5] ')
is there a library that does that ? or do i have to build it myself by traverssing the dom ?
Thanks in advance .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 这个 jQuery 插件 就可以了您正在寻找什么或至少接近它。
I think this jQuery plugin will do what you are looking for or at least close to it.
我喜欢切尔默茨的评论和托马斯·希尔兹。
综合考虑,一个好的方法是首先检查元素是否有 ID。如果是这样,请使用它 - 因为它始终是重新访问元素的最快方法。
如果它不返回 xpath 字符串。
另一种方法是简单地为其分配一个 ID。
我相信在 prototype.js 中你可以给元素一个标识。
I like chelmertz comment and Thomas Shields.
As a mixture of this, a good approach would be first to check if the element has an ID. If so, use that - as it's always going to be the fastest way to re-access your element.
And if it doesn't return an xpath string.
And alternative, would be to simply assign it an ID.
I believe in prototype.js you can give an element an identity.
你为什么要尝试传递该元素?你打算用它做点什么吗?如果没有,只需使用 jQuery,选择所有元素,然后选择您单击的元素。
例如,您正在谈论上面的表格行或单元格。您可以执行以下操作来选择特定的表格单元格:
或者将该代码中的
td
更改为tr
以使其将该类添加到该行。Why are you trying to pass the element? Do you intend on doing something with it? If not, just use jQuery, select all the elements and then choose the one you clicked.
For example, you're talking about table rows or cells above. You could do the following to select a specific table cell:
or change the
td
totr
in that code to make it add the class to the row.