导入:jquery.js + template-web.js
1.获取新闻列表数据
// 获取新闻列表的函数
function getNewsList() {
$.get('http://www.liulongbin.top:3006/api/news', function (res) {
if (res.status !== 200) {
return alert('获取新闻列表数据失败!')
}
for (var i = 0; i < res.data.length; i++) {
// 把每一项的 tags 属性,从字符串改造成字符串的数组
res.data[i].tags = res.data[i].tags.split(',')
}
console.log(res)
var htmlStr = template('tpl-news', res)
$('#news-list').html(htmlStr)
})
}
getNewsList()
2.定义模版
将#news-list中的标签放入script中
<!-- 定义模板 -->
<script type="text/html" id="tpl-news">
{
{each data}}
<div class="news-item">
<img class="thumb" src="{
{'http://www.liulongbin.top:3006' + $value.img}