方法一:
<script type="text/template" id="navTemp">
<% for(var i=0;i<items.length;i++){ %>
<li>
<a href="#">
<img src="<%=items[i].src%>" alt="">
<p><%=items[i].text%></p>
</a>
</li>
<% } %>
</script>
方法二:
<script type="text/template" id="navTemp">
{{each data as value index}}
<li>
<a href="#">
<img src="{{value.src}}" alt="">
<p>{{value.text}}</p>
</a>
</li>
{{/each}}
</script>
必写的代码:
<script src="./js/jquery.min.js"></script>
<script src="./js/template-native.js"></script>
<script>
$.ajax({
type:'get',
url:'http://day8.com/getnav.php',
//开启跨域请求
dataType:"jsonp",
success:function(result){
console.log(result);
var html=template("navTemp",{"items":result});
document.querySelector("ul").innerHTML=html;
}
})
</script>
作者:涅槃重飞
来源:CSDN
原文:https://blog.csdn.net/weixin_42663701/article/details/82561105