将HTML节点存储为* object *中的键 [英] Storing an HTML node as key in an *object*

查看:77
本文介绍了将HTML节点存储为* object *中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于这个SO问题,我想使用HTML节点作为对象(不是数组)中的键.

Similar to this SO question, I would like to use HTML nodes as keys in an object (not an array).

示例:

var _hotspots = {
                 [object HTMLDivElement] : { someProps:[1,2,3] },
                 [object HTMLDivElement] : { someProps:[1,2,3] }
                }

所以我可能会实现以下目标:

and so I might achieve things like this:

for( var a in _hotspots ){
    if(YAHOO.lang.hasOwnProperty(_hotspots, a)){
    alert('key nodeName: '+a.nodeName);
    }
}

到目前为止,当我在上面警告'a'时,它警告说它实际上是[object HTMLDivElement],所以看起来一切正常-但我无法访问'a'上的属性,例如nodeName.

So far, when I alert out 'a' above, it alerts out that it is in fact a [object HTMLDivElement], so it all seemed fine - but I can't access properties on 'a', like nodeName.

我正在做的可能吗?这是错的吗?如果我应该能够访问该对象的键引用上的属性,请告诉我,我将编写一个示例页面.

Is what I am doing possible? Is it wrong? If I should be able to access properties on the object's key reference then please let me know and I'll write up a sample page.

欢呼.

推荐答案

JavaScript对象的键始终是字符.您可以将id存储为键,然后在函数中检索元素.

The keys of JavaScript objects are always character. You could store the id as the key and then retreive the element in your function.

这篇关于将HTML节点存储为* object *中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆