JQuery implements click follow and cancel functions, while jquery implements click Cancel

Source: Internet
Author: User

JQuery implements click follow and cancel functions, while jquery implements click Cancel
Likes and network terms, indicating "agree" and "love ". This network language comes from the "like" function of the network community. The number of likes sent and received, the preference of likes, and so on can reflect to some extent what kind of person you are and what kind of status you are in. Behind the thumb ups, you are shown. The corresponding function is to cancel the function. It happens that a blogger is in an APP recently, and one of the sections needs to implement the like and cancel functions. After thinking about it, I decided to use JQuery code to implement it. This is just a simple change to the css style, okay, let's talk a little bit about it. Get it done !! First, introduce the JQuery plug-in.

Secondly, we need to define a div and give it some styles.

Then there is the JS Code, as shown below

$ (Document ). ready (function () {var onOff = true; var div = $ (". div "); div. click (function () {if (div. onOff) {div. val ("Follow me"); div.css ({"background": '# ccc'}); div. onOff = false;} else {div.css ({"background": 'red'}); div. val ("followed"); div. onOff = true ;}});});

The implementation result is as follows:

It can be seen that JS Code is easy to use. Similarly, image switching can also be achieved, as shown in figure

The implementation code is as follows:

Html:

<div class="div"></div>

Css: Pay attention to the image path

.div{                 background-image: url(img/guanzhu.png);                 width: 100px;height: 40px;background-size:80px;background-repeat: no-repeat;             }

JS Code

$(document).ready(function(){                var onOff=true;                var div=$(".div");                div.click(function(){                                if (div.onOff) {                          div.css({"background-image":'url(img/guanzhu.png)'});                          div.onOff = false;                        } else {                            div.css({"background-image":'url(img/yiguanzhu.png)'});                            div.onOff = true;                                }                    });              });

This is just a simple demo. Great gods with better ideas can talk a lot.

Okay, this is the time to share this article.

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.