jQuery 为 dom 元素提供唯一编号?
我记得有一个 jquery 方法为 dom 元素提供唯一的编号。它可能只适用于动画 dom 对象。现在我找不到那个方法了。那个方法是什么?还有其他方法可以为元素提供唯一编号吗?
I was remembering that there was a jquery method which provide unique number for dom elements. It just may for only animated dom objects. Now I couldn't find that method. What is that method ? Is there any another way to provide unique number for elements ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您可能正在考虑 jQuery.expando 的概念。每个运行 jQuery 的页面上都存在一个名为
jQuery.expando
的属性。它的定义是这样的:所以对我来说,在当前页面上,它是 jQuery15209244967177268291。任何存储有数据的元素(包括存储为数据的事件处理程序)都有一个具有该名称的属性。它包含一个唯一的编号,它是全局数据缓存中该元素的键。
例如,全局 StackExchange 收件箱位于屏幕左上角:
您可以使用
$('.genu')[0][jQuery.expando]
来模拟此操作;我不确定你是否会得到相同的号码。 (编辑:对我来说,甚至每次都不是相同的数字。)但是请注意,并非每个元素都有唯一的编号,只有那些附加了数据的元素才有唯一的编号。这可能适合也可能不适合您的目的......
I think you may be thinking about the concept of
jQuery.expando
. There is an attribute calledjQuery.expando
that exists on every page that has jQuery running. It is defined like this:So for me, on the current page, it is
jQuery15209244967177268291
. Any element that has any data stored on it (including event handlers, which are stored as data) has a property with that name. This contains a unique number, which is the key for that element in the global data cache.For instance, with the global StackExchange inbox on the top left of the screen:
You can mimic this with
$('.genu')[0][jQuery.expando]
; I'm not sure whether you'll get the same number. (Edit: it's not even the same number for me every time.)Note, however, that not every element has a unique number, only those with data attached to them. This may or may not fit your purposes...
唯一看起来像你可能想要的东西是 jQuery.unique(),但即使这样也达不到你的建议。我鼓励您更新您的问题以说明这样做的目的,因为可能有更好的方法来解决提示您获取每个元素的唯一编号的问题。
The only thing that looks remotely like what you might be after is the jQuery.unique(), but even that doesn't do what you're suggesting. I would encourage you to update your question to state the purpose for this, as there may be a better way to solve the problem that is prompting you to get unique numbers for each element.