jQuery Metadata

     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.
Arguments:
options (Optional) Options  
A set of key/value pairs that define the type of metadata to be extracted. All options are optional.
Options:
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.
Searches for metadata in a custom element attribute instead of in the class.
$(".selector").metadata({
   type: 'attr'
})
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.
Searches for metadata in a custom element attribute with a name of 'jdata'.
$(".selector").metadata({
   type: 'attr',
   name: 'jdata'
})
single String Default: 'metadata'
The name given to the data extracted from the element in the jQuery cache.
Stores and retrieves the data extracted into an item named 'jdata' in the jQuery cache.
$(".selector").metadata({
   single: 'jdata'
})

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>
Gets metadata from a child element.
<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.
Arguments:
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.
Arguments:
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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章