简体   繁体   English

jQuery ajax addClass问题

[英]Jquery ajax addClass issue

First sorry im a big beginner and just experimenting, and I made a similar wall like facebook with oembed. 首先对不起,我是一个大的初学者,只是做实验,我用oembed制作了类似facebook之类的墙。

And would like to add a like, and dislike button too. 并且也想添加一个“喜欢”和“不喜欢”按钮。

I started with the like button, it works, likes, and unlikes too, and the cookie saves the class value perfectly. 我从点赞按钮开始,它也可以工作,喜欢和不同,并且cookie可以完美地保存类的值。

My problems is the ajax call, so actually when I click on the like button it overwrites all anchors href val and adds a class to all not on what click. 我的问题是ajax调用,因此实际上,当我单击like按钮时,它会覆盖所有的anchors href val,并为所有单击的对象都添加一个类。

here is my code 这是我的代码

jquery jQuery的

var cookieLike = "like_"
$('a.like').each(function(){
    var id = $(this).attr('href'), cookieLiked = cookieLike + id;

    switch($.cookies.get(cookieLiked) ) {
        case "unliked":
            $(this).removeClass('btn-success');
        break;
        case "liked":
            $(this).addClass('btn-success');
        break;
    }
}).on('click', function(e){
    e.preventDefault()
    var likeId = $(this).attr('href');
    $.ajax({
        url: "<?php echo base_url(); ?>stream/like/" + likeId ,
        type: "post",
        data: likeId,
        dataType: "json",
        success: function(like)
        {

            if(like.likeStatus == "unliked") {
                $('a.like').attr('href', likeId).removeClass('btn-success');
                $.cookies.set(cookieLike + likeId, 'unliked');

            }else if(like.likeStatus == "liked") {
                $('a.like').attr('href', likeId).addClass('btn-success');
                $.cookies.set(cookieLike + likeId, 'liked');

            }

        }
    });

});

html html

<div class="stream-bottom">
    <a href="#" class=" btn btn-mini comment">Komment</a>
    <div class="pull-right like-options">
        <a href="<?php echo $sp->sid; ?>" class=" btn btn-mini like"><i class="icon-thumbs-up" title="tetszik"></i> </a>
        <a href="<?php echo $sp->sid; ?>" class=" btn btn-mini dislike"><i class="icon-thumbs-down" title="nem tetszik"></i></a>
    </div>
</div>

could please someone point out what i am missing? 可以请别人指出我所缺少的吗?

Bind the target element (the clicked link) and reference it in the success callback 绑定目标元素(单击的链接)并在成功回调中引用它

In the .on('click') callback 在.on('click')回调中

var $link = $(this);

In the success callback use 在成功回调中使用

$(this).attr('href', likeId)

instead of 代替

$('a.like').attr('href', likeId)

Maybe: 也许:

.on('click', function (e) {
    e.preventDefault();

    var button = $(this);
    var likeId = button.attr('href');

    $.ajax({
        url: "<?php echo base_url(); ?>stream/like/" + likeId,
        type: "post",
        data: likeId,
        dataType: "json",

        success: function (like) {
            if (like.likeStatus == "unliked") {
                button.removeClass('btn-success');
                $.cookies.set(cookieLike + likeId, 'unliked');
            } else if (like.likeStatus == "liked") {
                button.addClass('btn-success');
                $.cookies.set(cookieLike + likeId, 'liked');
            }

        }
    });
});

When you use $("a") or $("a.like") you are referring to the entire set of anchor tags or anchor tags with 'like' as the class name. 当使用$("a")$("a.like") ,是指整个锚标签或以'like'作为类名的锚标签。 To specifically use the anchor tag on which you have clicked use $(this) variable. 要专门使用您单击过的锚标记,请使用$(this)变量。

That will give you the element on which the event got generated and in this case the anchor tag on which you clicked. 这将为您提供生成事件的元素,在这种情况下,将为您提供单击的锚标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM