收藏功能前端实现代码

基于jQuery+Bootstrap+ajax+SSM 收藏功能前端实现

收藏功能需求

收藏功能需求:用户点击收藏按钮,前端通过一个五角星的方式,实现代码。
当用户未收藏时,显示空心的五角星;
当用户收藏时,显示实心的五角星;
在这里插入图片描述在这里插入图片描述

Bootstrap

https://v3.bootcss.com/components/
Bootstrap 提供了很多的组件图标,其中就包含的空心的五角星和实心的五角心。
在这里插入图片描述

当span的class 的名字定义为glyphicon glyphicon-star,前端显示实心五角心;
当span的class 的名字定义为glyphicon glyphicon-star-empty,前端显示空心五角心;

下面直接贴下代码吧

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>收藏功能</title>
<%
	pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
<script src="${APP_PATH }/static/js/jquery-3.3.1.min.js"></script>
<link href="${APP_PATH }/static/bootstrap-3.3.7/css/bootstrap.min.css"
	rel="stylesheet">
<script src="${APP_PATH }/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<button type="button" class="btn btn-default" id="btn_collect">
			<span class="glyphicon glyphicon-star-empty" id="btn_collect_icon" aria-hidden="true"></span>收藏
		</button>
	</div>
</body>
<script type="text/javascript">
$("#btn_collect").click(function(){
	var classname = $("#btn_collect_icon").attr("class");
	$("#btn_collect_icon").removeClass("glyphicon-star-empty glyphicon-star");
	if(classname == "glyphicon glyphicon-star-empty" ){
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star");
		alert("收藏成功");
	}else{
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star-empty");
		alert("取消收藏");
	}
});
</script>
</html>

代码解释

<script src="${APP_PATH }/static/js/jquery-3.3.1.min.js"></script>
<link href="${APP_PATH }/static/bootstrap-3.3.7/css/bootstrap.min.css"
	rel="stylesheet">
<script src="${APP_PATH }/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>

这三行非常重要,主要功能就是引入jQuery+Bootstrap的包,这个最好使用本地的,并且顺序不能乱了

<body>
	<div class="container">
		<button type="button" class="btn btn-default" id="btn_collect">
			<span class="glyphicon glyphicon-star-empty" id="btn_collect_icon" aria-hidden="true"></span>收藏
		</button>
	</div>
</body>

上面就比较简单了,就是在页面中,有一个按钮。默认是空心glyphicon-star-empty",当然后期写后端代码的时候需要提前的判断才行的。

<script type="text/javascript">
$("#btn_collect").click(function(){
	var classname = $("#btn_collect_icon").attr("class");
	$("#btn_collect_icon").removeClass("glyphicon-star-empty glyphicon-star");
	if(classname == "glyphicon glyphicon-star-empty" ){
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star");
		alert("收藏成功");
	}else{
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star-empty");
		alert("取消收藏");
	}
});
</script>

上面就是前段点击事件的主要逻辑

$("#btn_collect").click(function() 

当点击按钮时触发。

var classname = $("#btn_collect_icon").attr("class");

通过span的id,获取到class的名字

if(classname == "glyphicon glyphicon-star-empty" ){
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star");
		alert("收藏成功");
	}else{
		$("#btn_collect_icon").addClass("glyphicon glyphicon-star-empty");
		alert("取消收藏");
	}

如果是空心的则设置为实心
如果是实心的则设置为空心

上面我有一行代码没有讲解,就是 $("#btn_collect_icon").removeClass(“glyphicon-star-empty glyphicon-star”);

这个的意思是,移出掉 btn_collect_icon 的类名上,有的 glyphicon-star-empty 或者是 glyphicon-star。因为后面的语法是 $("#btn_collect_icon").addClass(“glyphicon glyphicon-star-empty”);就是添加类名,是在原来的基础上添加的。

github地址:https://github.com/zou1906524696/collect

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖丨学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值