jQuery Metadata

深入理解jQuery metadata插件
本文详细介绍了jQuery metadata插件的使用方法,包括如何提取DOM元素的元数据、设置默认类型和名称选项以及获取特定元素的数据。通过示例代码展示了如何在不同场景下灵活运用该插件。

     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:
typeStringDefault: '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'
})
nameStringDefault: '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'
})
singleStringDefault: '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:
typeString 
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.
nameString 
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:
elemElement 
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值