替代点击事件的绑定功能

时间:2015-03-17 20:30:57

标签: javascript jquery

我正在使用插件来添加或减少投票。我试图在替代点击锚标签上绑定函数。 这是html标记

<div class="voter">
    <a class="vote_added" data-id="25">Vote Up</a>
    <a class="vote_down" data-id="25">Vote Down</a>
</div>

这是javascript

jQuery(".voter a:first-child").on( "click", function() {

       if(jQuery(this).hasClass('vote_added')){
        console.log("click");
        var data = jQuery(this).data();
        console.log(data);
        votemeaddvote(data.id);
        jQuery(this).removeClass('vote_added');
        jQuery('.voter a:last-child').addClass('vote_down');
       }else{
        jQuery(this).off();
        console.log(" Turning Off --- Vote Up Feature");
       }
    });

    jQuery(".voter a:last-child").on( "click", function() {

       if(jQuery(this).hasClass('vote_down')){
        console.log("click");
        var data = jQuery(this).data();
        console.log(data);
        votemedownvote(data.id);
        jQuery(this).removeClass('vote_down');
        jQuery('.voter a:first-child').addClass('vote_added');
       }else{
        jQuery(this).off();
        console.log(" Turnig Off --- Vote Down Feature");
       }
    });

这段代码只用了一次,因为我正在使用.off();函数分离事件。我想要

如果我点击添加投票功能。

1) It should run a function (votemeaddvote(data.id));
2) If i click it again , It should get disable.

如果我点击向下投票功能。

1) It should run a function (votemedownvote(data.id));
2) if i click it again , It should get disable.
3) Add vote function should work if i click on add vote after clicking down vote i.e Vice Versa.

它的功能与boredpanda.com中的投票系统非常相似 我正在尝试为前端实现类似的东西。

0 个答案:

没有答案