最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

is there a way to add a variableproperty to a DOM element (eg, an element returned by a jQuery selector) - Stack Overflow

programmeradmin2浏览0评论

I am looking for an alternative to using an object/variable in global scope -- I would like to associate key/value pairs with specific DOM elements (eg, a DIV), so that I can use them as input for logic that processes other elements (eg, child elements of said DIV).

I tried something naive like: $('[foo=bar]').key='value' , and $('[foo=bar]')[key]='value', but it puked.

Doing something like: var foobar = $('[foo=bar]'); foobar.key = 'value' -- works, but the new property/value only affects the new object (ie, foobar, not $('[foo=bar]'))

Most likely there's something terribly basic I am overlooking. Any help is appreciated, thanks!

I am looking for an alternative to using an object/variable in global scope -- I would like to associate key/value pairs with specific DOM elements (eg, a DIV), so that I can use them as input for logic that processes other elements (eg, child elements of said DIV).

I tried something naive like: $('[foo=bar]').key='value' , and $('[foo=bar]')[key]='value', but it puked.

Doing something like: var foobar = $('[foo=bar]'); foobar.key = 'value' -- works, but the new property/value only affects the new object (ie, foobar, not $('[foo=bar]'))

Most likely there's something terribly basic I am overlooking. Any help is appreciated, thanks!

Share Improve this question asked Mar 9, 2010 at 0:00 btkbtk 3,2352 gold badges31 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

$().data() is what you seek

Here is a link to .data() in the API Documentation for jQuery.

Description: Store arbitrary data associated with the matched elements.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论