jquery metadata 详解

[size=large][color=red]1.0的版本是这样的$.meta
2.0的版本是这样的$.metadata[/color][/size]
很多插件的编写都用到了这个插件,个人感觉这个东西应该是jquery官方的。推荐使用2.0的版本,因为现在官方上就是2.0的文档([url]http://docs.jquery.com/Plugins/Metadata[/url]),1.0的,我个人还没看懂,倒是2.0的例子运行很正常。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="jquery.metadata.2.0/jquery.metadata.2.0/test/jquery.js"></script>
<!-- 这里既引用了1.0又引用了2.0,就是为了说明它们之间没有冲突,我的理解是以前的优秀插件里面使用了1.0,但是1.0的问题确实有,所以2.0把meta改成metadata 这样就不会相互冲突了-->
<script type="text/javascript" src="jquery.metadata.2.0/jquery.metadata.2.0/jquery.metadata.js"></script>
<script type="text/javascript" src="jquery.metadata.1.0/jquery.metadata.1.0/jquery.metadata.js"></script>
<script language="javascript">
$(document).ready(function(){
alert($(".media").metadata().src);
alert($('li.someclass').metadata().some);
});

</script>
</head>

<body>

<OBJECT class="media {src:'/tems/upload/192168.mp3'}" data="[object Object]"></OBJECT>
<li class="someclass {some: 'data'} anotherclass">...</li>
OR
<li data="{some:'random', json: 'data'}">...</li>
OR
<li><script type="data">{some:"json",data:true}</script> ...</li>
<script language="javascript">
var data = $('li.someclass').metadata();
if ( data.some && data.some == 'data' )
alert('It Worked!');
</script>
</body>
</html>


翻译:
attr:内部属性,参数名字指向属性名
class:内部类的属性,用{}包裹
elem:内部子元素 (如script标签).参数的名字指向元素名。

[b][size=medium]函数:[/size][/b]
[color=red][b]metadata( options )[/b] Returns: Object [/color]
Extracts, caches, and returns metadata from the first element in the jQuery collection.

[b]Arguments:[/b]
options (Optional) Options
A set of key/value pairs that define the type of metadata to be extracted. All options are optional.

[b]Options:[/b]
metadata( options )中的options有三种:type、name、single
[b][u]type[/u] String Default: 'class'[/b]
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'
})

[b][u]name[/u] String Default: 'metadata'[/b]
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'
})

[b][u]single[/u] String Default: 'metadata'[/b]
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'
})


[b]Examples:[/b]
[b]Gets metadata from the class attribute.[/b]
<li class="someclass {some: 'data'} anotherclass">...</li>

<script>alert($('li.someclass').metadata().some);</script>

[b]Gets metadata from a custom attribute.[/b]
<li data="{some:'random', json: 'data'}">...</li>
<script>alert($('li.someclass').metadata({type:'attr',name:'data'}).some);</script>

[b]Gets metadata from a child element.[/b]

<li class="someclass"><script type="application/json">{some:"json",data:true}</script>...</li>
<script>alert($('li.someclass').metadata({type:'elem',name:'script'}).some);</script>



[color=red][b]jQuery.metadata.setType( type, name )[/b] Returns: null [/color]
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:
* Code

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>



[color=red][b]jQuery.metadata.get( elem, options ) [/b]Returns: Object [/color]
Sets the default type and name options for all following metadata requests.

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:

* Code

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值