【问题标题】:What is the best way To make a Follow button Like Twitter Follow button制作关注按钮的最佳方法是什么,例如 Twitter 关注按钮
【发布时间】:2012-03-29 03:22:49
【问题描述】:

我想在 JQuery、Json 和 Ajax 中创建一个关注按钮,例如 Twitter 关注

我正在使用这种方式关注

 $(function() 
{
$("#follow").click(function(){
var content_id = $('#content_id').val();
var content_type = $('#content_type').val();

$.ajax({
type: "POST",
dataType: 'json',
url: "/memberactions/follow/",
async:false,
data:{content_id:content_id, content_type:content_type},
success: function(){
//$("#loading").ajaxComplete(function(){}).slideUp();
$('#follow').fadeOut(200).hide();
$('#unfollow').fadeIn(200).show();
}
});
return false;
});
});

这样取消关注

$(function() 
{
$("#unfollow").click(function(){
var I = $('#content_id').val();
var content_type = $('#content_type').val();

$.ajax({
type: "POST",
dataType: 'json',
url: "/memberactions/follow/",
async:false,
data:{content_id:content_id, content_type:content_type},
success: function(){
//$("#loading").ajaxComplete(function(){}).slideUp();
$('#unfollow').fadeOut(200).hide();
$('#follow').fadeIn(200).show();
}
});
return false;
});
});

这是隐藏关注按钮的最佳方式吗? 有更好的方法吗?

或者把一个属性following放在 喜欢

<a id="follow-an" href="#" following = "yes" >

我不想让关注按钮隐藏在页面中,避免关注和取消关注在同一个页面,当他关注时,只显示取消关注,当他不关注时,关注按钮显示了

【问题讨论】:

  • 我不明白这个问题。你到底想要什么?它似乎对我有用,看起来还不错:jsfiddle.net/xraeG
  • 它对我来说很好用,但我不想在页面中隐藏关注按钮,以避免在同一页面中同时关注和取消关注,当他关注时,只有取消关注是显示,当他没有关注时,会显示关注按钮,希望这样可以更清楚
  • 所以你想要这样的东西? jsfiddle.net/xraeG/2
  • 没错,谢谢它的工作,我将转发操作取决于 attr
  • 没问题,很高兴为您提供帮助。作为答案发布,因此您可以根据需要接受。

标签: javascript jquery ajax


【解决方案1】:

我希望这是你想要的:

HTML:

<button id="button" data-following="false">Follow</button>

JS:

$("#button").click(function(){
    if($(this).attr('data-following') == 'false'){
        $(this).attr('data-following', 'true');
        $(this).text('Unfollow');
    }else if($(this).attr('data-following') == 'true'){
        $(this).attr('data-following', 'false');
        $(this).text('Follow');
    }
});

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 2011-09-08
    • 1970-01-01
    • 2016-02-14
    • 2011-07-08
    • 1970-01-01
    • 2021-10-04
    • 2018-04-20
    • 2012-09-02
    相关资源
    最近更新 更多