jQuery中的收藏夹和收藏夹按钮不起作用 [英] Favorite and unfavorite button in Jquery is not working

查看:48
本文介绍了jQuery中的收藏夹和收藏夹按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个按钮来帮助用户喜欢或不喜欢的文章.当用户单击按钮时,按钮或背景的颜色需要更改为橙色,而当他不喜欢该文章时,按钮上的颜色应消失.

I need to develop a button that helps users favorite or un-favorite an article. When the user clicks on the button, the color of the button or the background needs to change to Orange, and when he un-favorites the article, the color on the button should go away.

这是我当前在Jquery中的实现,但仅能运行一次.绑定不会第二次处理.

Here's my current implementation in Jquery, but it only works once. The binding is not handled the second time.

我想在第二次创建元素时必须再次处理绑定,但是我不确定在哪里处理.

I suppose I have to handle the binding again when the elements are created for the second time, but I am not exactly sure where to handle it.

    $('.colorStar').bind('click', function() {

            var id = $(this).parent().parent().parent().parent().attr('id');
            var removeElement = $(this).parent();
             $(removeElement).empty();
             $("<button data-action='show-contribute-how-to' class='btn colorStarred show'><i class='icon-star'></i></button>")
             .click(function (){
                var id = $(this).parent().parent().parent().parent().attr('id');
                var removeElement = $(this).parent();
                $(removeElement).empty();
                $(removeElement).append("<button data-action='show-contribute-how-to' class='btn colorStar show'><i class='icon-star'></i></button>");  
             }).appendTo(removeElement);

            $.ajax({
                type : "POST",
                url : "starNote",
                data : {
                    id : id
                }
            });
        });

HTML

<a class="star"><button class="btn colorStar hide" data-action="show-contribute-how-to"><i class="icon-star"></i></button></a>

http://jsfiddle.net/SLq8W/

http://jsfiddle.net/SLq8W/

推荐答案

删除事件:

$('body').on('click', '.colorStar', function() {

代替

$('.colorStar').bind('click', function() {

这篇关于jQuery中的收藏夹和收藏夹按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆