简体   繁体   English

用jQuery更改文本和类

[英]Changing text and class with jquery

I've been stuck all day on a project I'm working on. 我整日忙于我正在从事的项目。 All funcitonality is there, I'm just having trouble with the jquery, can someone help me out? 所有功能都在那里,我只是在使用jquery时遇到麻烦,有人可以帮我吗?

here is a visual of the application: 这是应用程序的外观:

http://imgur.com/nsdgubB

Pretty simple, when I click "like post" a function is called via ajax to run a update to the count on the database, also the same when I press "Dislike" 很简单,当我单击“喜欢发布”时,通过ajax调用一个函数来运行数据库计数的更新,当我按下“不喜欢”时也是如此

My problem is with the JQuery, I can't seem to figure out how to make it so when "Like post" is clicked, it is changed to "Already Liked" and the "Already Disliked" changed to "Dislike" 我的问题与JQuery有关,我似乎无法弄清楚如何做到这一点,因此,当单击“赞帖子”时,将其更改为“已喜欢”,而将“已不喜欢”更改为“不喜欢”

CSS Classes: CSS类:

    "Like Post" = fa fa-thumbs-up like_btn
    "Dislike Post" = fa fa-thumbs-down dislike_btn
    "Already Liked" = fa fa-check like_btn
    "Already Disliked" = fa fa-check fa-check-dislike dislike_btn"

Here is the current JQuery, I've tried a few different things but nothing seems to be working, any suggestions I greatly appreciate. 这是当前的JQuery,我尝试了一些不同的操作,但似乎没有任何效果,任何建议我都非常感谢。

$('.fa-thumbs-up').click(function() {

var annid = $(this).attr('id');
update_likes(annid, 'like');
//code goes here
});

$('.fa-thumbs-down').click(function() {

var annid = $(this).attr('id');
update_likes(annid, 'dislike');
//code goes here
});

UPDATED JFIDDLE: http://jsfiddle.net/T9wvL/2/ 更新的JFIDDLE: http : //jsfiddle.net/T9wvL/2/

It's easier to toggle elements than to swap text, so I modified your HTML a bit. 切换元素比交换文本更容易,因此我对HTML进行了一些修改。

http://jsfiddle.net/isherwood/T9wvL/8 http://jsfiddle.net/isherwood/T9wvL/8

.hide {
    display: none;
}

<article>
    <div class="updates updates-like">
        <i class="fa fa-thumbs-up like_btn" id="like"> 
            <span>Like Post</span>
            <span class="hide">Already liked</span>
        </i> 
    </div>

    <div class="updates updates-dislike">
        <i class="fa fa-thumbs-down dislike_btn" id="dislike">
            <span>No Good</span>
            <span class="hide">Already disliked</span>
        </i>
    </div>
</article>

$('.updates').click(function () {
    $(this).find('i').toggleClass('fa-thumbs-up fa-thumbs-down')
        .find('span').toggleClass('hide');
});

HTML: HTML:

<article>
    <div class="updates-like">
        <i class="fa fa-thumbs-up like_btn" id="<? echo $update->annid; ?>">
            <span>Like Post </span>
        </i> 
    </div>
    <div class="updates-dislike">
        <i class="fa fa-thumbs-down dislike_btn" id="<?echo $update->annid;?>">
            <span>No Good</span>
        </i> 
    </div>
</article>

JQuery: JQuery的:

$('.like_btn').click(function () {
    /*This here runs the update function on the DB.. Not Important
       var annid = $(this).attr('id');
       update_likes(annid, 'like');*/

        //Code here to change "Like Post" to "already Liked" ON CLICK, and to change "already disliked" back to "No Good"

    if ( $(this).hasClass('fa-thumbs-up') ) {
        $(this).removeClass('fa-thumbs-up')
               .addClass('fa-check')
               .find('span')
               .text('Already Liked');
        if ( $('.dislike_btn').hasClass('fa-check fa-check-dislike') ) {
            $('.dislike_btn').removeClass('fa-check fa-check-dislike')
                             .addClass('fa-thumbs-down')
                             .find('span')
                             .text('No Good');
        }
    }

    return true;

});

$('.dislike_btn').click(function () {
    /*This here runs the update function on the DB.. Not Important
        var annid = $(this).attr('id');
        update_likes(annid, 'dislike'); */

        //Code here to change "No Good" to "already disliked" ON CLICK, and to change "already Liked" back to "No Like Post"

    if ( $(this).hasClass('fa-thumbs-down') ) {
        $(this).removeClass('fa-thumbs-down')
               .addClass('fa-check fa-check-dislike')
               .find('span')
               .text('Already Disliked');
        if ( $('.like_btn').hasClass('fa-check') ) {
            $('.like_btn').removeClass('fa-check')
                          .addClass('fa-thumbs-up')
                          .find('span')
                          .text('Like Post');
        }
    }

    return true;

});

Here is the JSFiddle that should work for what you need. 这是应该满足您需求的JSFiddle I tried to optimize a little bit your code, but I hope it still works fine. 我试图优化您的代码,但我希望它仍然能正常工作。

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

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