web前端如何将js数据在页面展示

web前端如何将js数据在页面展示

  在获取到json数据后,在html中用script引入json数据,并进行页面布局:

   可以先进行html的初步页面布局,在使用json数据中的一个对其进行css 样式设置,设置好后删除,最后在script引入json数据,这样的话引入的数据就会样式一致,减少了在script中设置样式的麻烦.

例子:

json数据中的一个

"ablum": "我们不一样",
	"artist": "大壮",
	"id": 23,
	"name": "大壮 - 我们不一样",
	"path": "musics/8/1551273973067.mp3",
	"size": 10935843,
	"style": "流行",
	"uid": 8,
	"uploadtime": 1551273973000

html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<!-- 引入页面布局的css -->
		<link rel="stylesheet" href="css/exp2.css">
		<!-- 引入字体样式 -->
		<link rel="stylesheet" type="text/css" href="asserts/font-awesome-4.7.0/css/font-awesome.min.css" />
	</head>
	<body>

		<div class="header">
			<div class="header-img"><img class="img1" src="img/logo1.bmp" alt=""></div>
			<div class="a-type"><a class="a1" href="">发现</a></div>
			<div class="a-type"><a class="a1" href="">我的音乐</a></div>
			<div class="a-type"><a class="a1" href="">音乐人</a></div>
			<div class="a-type"><a class="a1" href="">客户端下载</a></div>
			<div class="a-type"><a class="a1" href="">会员中心</a></div>

			<div class="input"><input type="text" placeholder="搜索"><i class="fa fa-search"></i></div>
			<div class="btn"><button type="button">登录/注册</button></div>
		</div>
		<div class="main">
			<div class="left">
				<div class="left-img"><img class="img2" src="img/10.jpg" alt=""></div>
			</div>
			<div class="right">
				<div class="item">
					<div class="item-id">12</div>
					<div class="item-img"><img class="img3" src="img/1.jpg"></div>
					<div class="item-name">小苹果</div>
					<div class="item-artist">筷子兄弟</div>
					<div class="item-style">流行</div>
					<div class="item-size">3.4MB</div>
				</div>
			</div>
		</div>
		<div class="footer">
			<div class="btn2"><button class="btn2-type" type="button">播放</button></div>
		</div>
       <!--引入json数据  -->
		<script src="js/list.js"></script>
		<script>
			var html = '';
			for (var i = 0; i < musics.length; i++) {
				//获取一首歌曲
				var m = musics[i];

				html += '<div class="item">';
				html += '<div class="item-id">' + m.id + '</div>';
				html += '<div class="item-img"><img class="img3" src="img/1.jpg"></div>';
				html += '<div class="item-name">' + m.name + '</div>';
				html += '<div class="item-artist">' + m.artist + '</div>';
				html += '<div class="item-style">' + m.style + '</div>';
				html += '<div class="item-size">' + fmtsize(m.size) + '</div>';
				html += '</div>';
			}
			document.querySelector('.right').innerHTML = html;

			function fmtsize(size) {
				size = size / (1024 * 1024);
				size = size.toFixed(1);
				return size + 'MB';
			}
		</script>
	</body>
</html>

css

/* 界面整体框架结构 */

*{
	margin: 0;
	padding: 0;
	
}
body {
	
	width: 1350px;
	height: 635px;

}

div {
	box-sizing: border-box;
}

.header,
.footer{
	position: relative;
	width: 100%;
	height:70px;
}

.main{
	position: relative;
	width: 100%;
	height:495px;
}
.header{
	list-style: none;
}
.footer{
	bottom: 0;
}

.left,
.right{
	position: absolute;
	float: left;
	height:495px;
}
.left {
	left: 0;
    width: 25%;
}

.right {
	
	left: 25%;
	right: 5%;
	width: 70%;
	overflow-y: scroll;
}

/* 头部样式*/
.header-img{
	position: relative;
	float: left;
	width: 200px;
}

.img1{
	position: relative;
	float: right;
	top: 10px;
	width: 120px;
	height: 50px;
}
.a-type{
	float: left;
	width: 90px;
	height: 70px;
	text-align: center;
	line-height: 70px;
}
.a1{
	display: block;
	text-decoration: none;
	color: #222222;
}
.a1:hover{
	color:#0000FF;
}
.input{
	position: absolute;
	right: 150px;
	width:170px;
	height: 70px;
	text-align: center;
	line-height: 70px;
}
.btn{
	position: absolute;
	right: 50px;
	width: 90px;
	height: 70px;
	text-align: center;
	line-height: 70px;
	
}
/* 左侧样式 */
.left-img{
	position: relative;
	top: 80px;
	left: 100px;
	height: 180px;
	width: 180px;
	
}
.img2{
	height: 180px;
	width: 180px;
}
/* 右侧样式 */
.item{
	display: flex;
	height: 50px;
	line-height: 50px;
	color: #556766;	
}

.item:nth-child(2n+1){
	background-color: #ddd;
}
/* .item:nth-child(2n){
	background-color: #bbb;
} */

.item:hover{
	background: #2980B9;
}
.item-id{
	flex: 1;
}
.item-img{
	flex: 1;
		margin: 5px auto;
}
.img3{
	width: 40px;
	height: 40px;
}
.item-name{
	flex: 4;
	padding-left: 10px;
}
.item-artist{
	flex: 2;
	padding-left: 10px;
}
.item-style{
	flex: 3;
	padding-left: 10px;
}

.item-size{
	flex: 1;
} 
/* 底部样式 */
.btn2{
	position: relative;
	left: 50%;
	margin: 0 0 0 -100px;
	height: 100%;
	width: 200px;
}
.btn2-type{
	position: relative;
	left: 30px;
	height: 70px;
	width: 140px;
	font-size: 18px;
	text-align: center;
	line-height: 70px;
	color: #eee;
	background: rgba(176,54,255,0.5);
	
	
}
.btn2-type:hover{
	background: #3498db;
}


 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值