jQuery 为 dom 元素提供唯一编号?

发布于 2024-12-04 19:03:32 字数 95 浏览 0 评论 0原文

我记得有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱人如己 2024-12-11 19:03:32

我想您可能正在考虑 jQuery.expando 的概念。每个运行 jQuery 的页面上都存在一个名为 jQuery.expando 的属性。它的定义是这样的:

expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),

所以对我来说,在当前页面上,它是 jQuery15209244967177268291。任何存储有数据的元素(包括存储为数据的事件处理程序)都有一个具有该名称的属性。它包含一个唯一的编号,它是全局数据缓存中该元素的键。

例如,全局 StackExchange 收件箱位于屏幕左上角:

$('.genu')[0].jQuery15209244967177268291 === 29

您可以使用 $('.genu')[0][jQuery.expando] 来模拟此操作;我不确定你是否会得到相同的号码。 (编辑:对我来说,甚至每次都不是相同的数字。)

但是请注意,并非每个元素都有唯一的编号,只有那些附加了数据的元素才有唯一的编号。这可能适合也可能不适合您的目的......

I think you may be thinking about the concept of jQuery.expando. There is an attribute called jQuery.expando that exists on every page that has jQuery running. It is defined like this:

expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),

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:

$('.genu')[0].jQuery15209244967177268291 === 29

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...

不羁少年 2024-12-11 19:03:32

唯一看起来像你可能想要的东西是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文