jQuery metadata可以说是jQuery插件的插件,用jQuery写插件的朋友都知道,大多数情况下我们要收入jQuery.metadata,它是出自jQuery官方的,2.0版本以后的都用起来很方便,具体作用是从dom对象提取元数据并返回此对象,要想会用的话,那可以好好看看官方文档了。
下面是官方的文档:
metadata( options ) | Returns: Object |
Extracts, caches, and returns metadata from the first element in the jQuery collection. | |
jQuery.metadata.setType( type, name ) | Returns: null |
Sets the default type and name options for all following metadata requests. | |
jQuery.metadata.get( elem, options ) | Returns: Object |
Extracts metadata from an element. |
1、metadata(options)
Extracts, caches, and returns metadata from the first element in the jQuery collection.
Extracts, caches, and returns metadata from the first element in the jQuery collection.
options (Optional) | Options | |
---|---|---|
A set of key/value pairs that define the type of metadata to be extracted. All options are optional. |
type | String | Default: 'class' |
---|---|---|
Specify the expected locations of metadata for the element. Possible values are 'class': search in the class attribute, 'elem': search for an element inside the element being searched, and 'attr': search in a custom attribute on the element. | ||
name | String | Default: 'metadata' |
When type is 'attr', specify the name of the custom attribute for which to search. When type is 'elem', specify the tag name of the element for which to search. | ||
single | String | Default: 'metadata' |
The name given to the data extracted from the element in the jQuery cache. | ||
Examples:
Gets metadata from the class attribute.
<li class="someclass {some: 'data'} anotherclass">...</li> <script>alert($('li.someclass').metadata().some);</script>
Gets metadata from a custom attribute.
<li data="{some:'random', json: 'data'}">...</li> <script>alert($('li.someclass').metadata({type:'attr',name:'data'}).some);</script>
<li class="someclass"><script type="application/json">{some:"json",data:true}</script>...</li> <script>alert($('li.someclass').metadata({type:'elem',name:'script'}).some);</script>2、jQuery.metadata.setType(type,name)
Sets the default type and name options for all following metadata requests.
type | String | |
---|---|---|
Specify the expected location of metadata for the element. Possible values are 'class' (default): search in the class attribute, 'elem': search for an element inside the element being searched, and 'attr': search in a custom attribute on the element. | ||
name | String | |
The name of the tag or attribute for which to search depending on the value of the 'type' option. |
Examples:
Setup metadata plugin to look for a custom attribute.<li data="{some:'random', json: 'data'}" class="someclass">...</li> <script> $.metadata.setType('attr','data'); alert($('li.someclass').metadata().some); </script>
3、jQuery.metadata.get( elem, [options] )
Extracts metadata from an element.
elem | Element | |
---|---|---|
The element containing the metadata to be extracted. | ||
options (Optional) | Options | |
A set of key/value pairs that define the type of metadata to be extracted. All options are optional. See the metadata plugin page for more information. |
Examples:
Setup metadata plugin to look for a custom attribute.<li class="someclass {some:'random', json: 'data'}">...</li> <script> $('li.someclass').each(function(){ var data = $.metadata.get(this); alert(data.some); }); </script>