有时候,刷新页面的时候,我们喜欢的是使用string构造出一个字符串,然后再一直的拼接拼接再拼接,最后innerhtml到页面上,很明显,我也是这样做的,省事呀!
后面找了下这方面的材料。找到了一个arttemplate的插件,是一个兄弟在github上弄的,所以就直接拿过来用了,毕竟很多项目是没有用到类似vue的。
连接:
代码如下:
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<style>
.big_title {
text-decoration: none;
width: 80%;
display: block;
color: #ffffff;
text-align: center;
font-size: 16px;
height: 40px;
line-height: 40px;
margin-left: 10%;
margin-right: 10%;
margin-top: 20px;
border-radius: 5px;
/*background: #000;*/
}
</style>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body id="body" style="width: 100%;">
</body>
</html>
<!--列表加载的脚本-->
<script id="company_list" type="text/template">
<%for(let i=0;i<list.length;i++){%>
<a class="big_title">
<%=list[i]%>
</a>
<%}%>
</script>
<script type="text/javascript" src="../js/jquery.min.js" ></script>
<script type="text/javascript" src="../js/arttmpl.js"></script>
<script type="text/javascript" src="../config.js"></script>
<script>
var strData = template('company_list', {
"list": company
});
$("body").append(strData);
//设置不同颜色的背景色
$(".big_title").each(function(index) {
var color_str = ""; //默认颜色背景为000
color_str += "rgb("; //注意写法,如果有rgba则要加上透明度
for(var i = 0; i < 3; i++) {
var a = Math.random() * 255;
a = Math.ceil(a);
color_str += a;
color_str += ",";
}
color_str = color_str.substr(0, color_str.length - 1);
color_str += ")";
console.log(color_str);
$(this).css("background-color", color_str);
});
</script>
其中的config.js是一个简单的文件,里面只有:
var company = [
"宏略信息科技有限公司"
// "向往创投有限公司"
]; //公司列表
就这么简单,也不怎复杂,毕竟不需要弄的太麻烦,