English 中文(简体)
使用 jquery 的 Facebook 按钮
原标题:Facebook like button using jquery

我试图做一张像按钮一样的Facebook。这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>like button</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<style>
    #like {
        height: 215px;
        background: url(images/sprite.png) no-repeat;
        background-position: 0px 0px;
    }
</style>
<script type="text/javascript">
    $(document).ready(function(){
        $("#like").click(function(){
            $("#like").css("background-position","0px -225px");
        });
    });
</script>
</head>
<body>
    <div id="like">
    </div>
</body>
</html>

现在,当用户点击该图片时,图片会改变。我想改变该图片,反之亦然。我怎么能呢?还有任何其他完美的方法可以执行吗?

最佳回答

定义 2 CSS 类( 1 为活动状态, 2 为其他状态), 并有相关的 CSS 类( 从图示图像装入图像) 。 在按钮/ 类似图像点击事件上使用这些 。 类似 类似 的 类似 。

<强 > CSS

.stateA,.stateB
{
  background-image:url( http://www.wing-co.jp/img/ui/site_skin/default/sprite_facebook.png );
  width:20px;  height:20px;       
}

.stateA
{  
  background-position:-511px -212px;    
}
.stateB
{
  background-position:-498px -212px;    
}

< 强 > HTML

<input type="button" id="btnLike" class="stateA" value="" />

< 强力 > 标注

$(function(){

    $("#btnLike").click(function(e){
      e.preventDefault();
      var item=$(this);
      if(item.attr("class")=="stateA")
      {
         item.removeClass("stateA").addClass("stateB");
         //If you want to update database,you can make an ajax call here
      }
      else
      {
         item.removeClass("stateB").addClass("stateA");
         //If you want to update database,you can make an ajax call here
      }
  });   

});

工作样本:http://jsfiddle.net/cGWTM/32/

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签