jQuery对象选择器作为字符串 [英] Jquery object selector as string
问题描述
有什么方法可以获取jquery对象的选择器
Is there any way to get the selector for a jquery object
例如在Firefox中,我看到一个jquery对象为[p.basket]
e.g in firefox I see a jquery object as [p.basket]
但是在jquery中似乎没有办法可以得到这个选择器?
but there seems to be no way in jquery that I can get this selector?
有什么办法吗?
菲尔
推荐答案
如果使用选择器字符串创建了jQuery对象,则只需查看其"selector"属性即可.但是,并非所有的jQuery对象都是这样构造的.因此,您应该确保检查null
.
If a jQuery object was created with a selector string, then you can just look at its "selector" property. However, not all jQuery objects are so constructed. Thus you should make sure to check for null
.
edit —如果您的jQuery对象不是使用选择器构造的,则根本就没有选择器可用.该库没有任何内置方法来创建一个与其包含的元素集相匹配的选择器.您可以自己做,尽管尚不清楚为什么会有用.一旦有了对DOM元素的引用(如果jQuery对象不为空,该操作就可以了)吗?
edit — if your jQuery object was not constructed with a selector, then there simply is not a selector available. The library does not have any built-in way of creating a selector that matches the set of elements it contains. You could do that yourself, though it's not clear why it would be useful; once you have a reference to the DOM elements (which you do if the jQuery object isn't empty), isn't that more useful?
这篇关于jQuery对象选择器作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!