Home > Web Front-end > JS Tutorial > How to add 1 like with jQuery

How to add 1 like with jQuery

藏色散人
Release: 2021-11-17 11:58:40
Original
2597 people have browsed it

How to add 1 like with jQuery: 1. Import jquery; 2. Set HTML and CSS code; 3. Pass the code "$(".liker").on("click",function() {...}" method can achieve the effect of adding 1 likes.

How to add 1 like with jQuery

The operating environment of this article: Windows 7 system, jquery-3.3.1 version, DELL G3 Computer

How to add 1 like with jQuery?

Jquery one Simple like effect, achieve the number of likes 1

1, import JS

<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
Copy after login

2, CSS code

a{
	text-decoration:none ;
	color: #999999;}.red{
	color: red;}
Copy after login

3, HTML code

<p>王思葱竟然也是舔狗?<a href="#"><span class="liker">❤999</span></a></p><p>震惊,一名科学家发现每刷一分钟抖音,人的生命就会减少60秒!<a href="#"><span class="liker">❤520</span></a></p>
Copy after login

4. JS code

$(function(){
	//点赞
	$(".liker").on("click",function(){
		// 判断是否已经点赞过
		if($(this).hasClass("red")){
			return
		}
		//添加样式 并且数字+1
		$(this).addClass("red")
		var num=$(this).html().substring(1)
		num++
		$(this).html("❤"+num)
		//ajax更新数据
	})})
Copy after login

Run...
How to add 1 like with jQuery
After clicking the little heart
How to add 1 like with jQuery

Of course, there is no background interaction written here Code

Recommended study: "jquery video tutorial"

The above is the detailed content of How to add 1 like with jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template