Skip to content

Instantly share code, notes, and snippets.

@sword-jin
Created October 5, 2015 07:18
Show Gist options
  • Select an option

  • Save sword-jin/f119dd88cb1cf761ea76 to your computer and use it in GitHub Desktop.

Select an option

Save sword-jin/f119dd88cb1cf761ea76 to your computer and use it in GitHub Desktop.
jQuery 的简单模版
<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