1.通过a标签实现页面的跳转
index
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="ajax/index.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<style>
.item{
border:1px solid #333;
}
.item img{
width:100px;
height:100px
}
</style>
</head>
<body>
<div id="app">
</div>
<script>
var url = 'http://47.108.197.28:3000/top/playlist?cat=华语';
$ajax({
url,
success:res=>{
console.log(res.playlists)
var playlists = res.playlists;
playlists.forEach((item,index)=>{
var {name,id,coverImgUrl} = item;
var template = `
<a href='detail.html?id=${id}'>
<div class='item'>
<img src='${coverImgUrl}'/>
<p>${name}</p>
</div>
</a>
`
$("#app").append(template)
})
}
})
</script>
</body>
</html>
detail
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="ajax/index.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<style>
img{
width:200px;
height: 200px;
}
</style>
</head>
<body>
<div id="app">
</div>
<script>
/* 1、获取id值 */
var id = location.search.split("=")[1];
// console.log(id)
/* 2、根据id值发送http请求 */
var url = `http://47.108.197.28:3000/playlist/detail?id=${id}`;
$ajax({
url,
success:res=>{
console.log(res.playlist)
var {name,coverImgUrl,description} = res.playlist;
var template = `
<div>
<img src='${coverImgUrl}'/>
<p>${name}</p>
<P>${description}</p>
</div>
`
$("#app").append(template)
}
})
</script>
</body>
</html>
2.通过点击事件跳转
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="ajax/index.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<style>
.item{
border:1px solid #333;
}
.item img{
width:100px;
height:100px
}
</style>
</head>
<body>
<div id="app">
</div>
<script>
var url = 'http://47.108.197.28:3000/top/playlist?cat=华语';
$ajax({
url,
success:res=>{
var playlists = res.playlists;
playlists.forEach((item,index)=>{
var {name,id,coverImgUrl} = item;
var template = `
<div class='item' data-aid=${id}>
<img src='${coverImgUrl}'/>
<p>${name}</p>
</div>
`
$("#app").append(template)
})
$(".item").click(function(event){
var {aid} = event.currentTarget.dataset;
console.log(aid)
location.href = `detail.html?id=${aid}`
})
}
})
</script>
</body>
</html>