浏览 12139 次
锁定老帖子 主题:jquery metadata 详解
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-16
最后修改:2009-03-21
1.0的版本是这样的$.meta
2.0的版本是这样的$.metadata 很多插件的编写都用到了这个插件,个人感觉这个东西应该是jquery官方的。推荐使用2.0的版本,因为现在官方上就是2.0的文档(http://docs.jquery.com/Plugins/Metadata),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标签).参数的名字指向元素名。 函数: metadata( options ) Returns: Object 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: metadata( options )中的options有三种:type、name、single 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> jQuery.metadata.setType( type, name ) Returns: null 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> jQuery.metadata.get( elem, options ) Returns: Object 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> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |