55 lines
1.2 KiB
HTML
55 lines
1.2 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Title</title>
|
||
</head>
|
||
<body>
|
||
|
||
|
||
<button>收藏</button>
|
||
|
||
|
||
|
||
<script src='//cdn.bootcss.com/jquery/3.1.1/jquery.min.js'></script>
|
||
|
||
<script>
|
||
|
||
// 当前要收藏的数据
|
||
const data = JSON.parse(localStorage.getItem("info"));
|
||
var likeData = [];
|
||
document.write(JSON.stringify(data))
|
||
|
||
$("button").on('click', function () {
|
||
console.log("status")
|
||
|
||
if (localStorage.getItem("like") !== null) {
|
||
console.log("status2")
|
||
// 收藏的数据
|
||
const like = JSON.parse(localStorage.getItem("like"));
|
||
var status = false // true 重复,false 不重复
|
||
|
||
for (var i = 0;i<like.length;i++) {
|
||
if (like[i].id === data.id) {
|
||
status = true
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (status) {
|
||
console.log("重复")
|
||
} else {
|
||
like.push(data)
|
||
localStorage.setItem("like", JSON.stringify(like))
|
||
}
|
||
|
||
|
||
} else {
|
||
likeData.push(data)
|
||
localStorage.setItem("like", JSON.stringify(likeData))
|
||
}
|
||
});
|
||
|
||
</script>
|
||
</body>
|
||
</html> |