从 jQuery 对象获取哈希字符串/guid
我想要一个 jQuery 对象的哈希表,并且需要它们的某种唯一标识符。
DOM 表示只是
<tr>
...
</tr>
我想将 jQuery 对象添加到哈希表
hash[$(trObject).guid()] = $(trObject);
如何在不扩展 jQuery 的情况下唯一标识特定的 jQuery 对象?
我知道我可以编写一个方法
(function() {
var guid = 0;
$.fn.guid = function() {
var node = this[0];
if (node.guid === undefined) {
node.guid = guid++;
}
return node.guid;
};
}());
来为我做到这一点,但我更想知道是否有某种本机标准方法可以从 jQuery 对象获取字符串/整数哈希码。
PS 我不希望达到 32 位整数限制。
I would like to have a hashtable of jQuery objects and needs some kind of unique identifier for them.
The DOM representation is just
<tr>
...
</tr>
And I would like to add my jQuery <tr>
objects to a hashtable
hash[$(trObject).guid()] = $(trObject);
How can I uniquely identify a particular jQuery object without extending jQuery?
I know I could write a method
(function() {
var guid = 0;
$.fn.guid = function() {
var node = this[0];
if (node.guid === undefined) {
node.guid = guid++;
}
return node.guid;
};
}());
To do this for me, but I would prefer to know if there is some kind of native standard way to get a string/int hashcode from a jQuery object.
P.S. I don't expect to hit the 32bit integer limit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有本机标准方法,但有用于此的 jquery 插件
例如。 http://plugins.jquery.com/project/GUID_Helper
更新:
jquery使用的guid号存储在
jquery.guid
此外,jquery 没有为此使用任何本机标准方法。它也遵循与您类似的方法。
No native standard way which I am aware of but there are jquery plugins for this
eg. http://plugins.jquery.com/project/GUID_Helper
UPDATE:
The guid number used by jquery is stored in
jquery.guid
Moreover, jquery is not using any native standard method for this. It is also following an approach similar to yours.