Created
October 5, 2015 07:18
-
-
Save sword-jin/f119dd88cb1cf761ea76 to your computer and use it in GitHub Desktop.
jQuery 的简单模版
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <body> | |
| <script id="blogTemplate" type="app/template"> | |
| <h2> {{title}} </h2> | |
| <img src="{{thumbnail}}" alt="{{title}}"/> | |
| </script> | |
| <script> | |
| (function($) { | |
| var content = [ | |
| { | |
| title: 'My awesome blog post', | |
| thumbnail: 'http://img.mukewang.com/5608e07f0001115106000338-240-135.jpg' | |
| }, | |
| { | |
| title: 'My second blog post', | |
| thumbnail: 'http://img.mukewang.com/55a5f6c100016bb806000338-240-135.jpg' | |
| } | |
| ], | |
| template = $.trim($('#blogTemplate').html()), | |
| html = ''; | |
| $.each(content, function(index, obj) { | |
| html += template.replace(/{{title}}/ig, obj.title) | |
| .replace(/{{thumbnail}}/ig, obj.thumbnail); | |
| }); | |
| // 遍历完成之后进行dom操作 | |
| $('body').append(html); | |
| })(jQuery); | |
| </script> | |
| </body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment