Jquery之Bind方法参数传递与接收的三种方法

来源:互联网 发布:中国不高兴知乎 编辑:程序博客网 时间:2024/05/21 01:55


方法一、

function GetCode(event)
{
alert(event.data.foo);
}






$(document).ready(function()
{
$("#summary").bind("click", {foo:'abc'} ,GetCode);
});

方法二、

函数句柄

$("#summary").bind("click", function()
{
GetCode("abc")
});




function GetCode(str)
{
}
方法三、

函数闭包

function GetCode(str)
{
return function()
{
alert(str)
}}


$("#summary").bind("click", GetCode("abc"));



/**
 * 赞同评论
 */
function commentLike(cid) {
 //alert("innser = " + cid);
 $("#comment-agree-" + cid).unbind("click");
 $.ajax({
  type : "POST",
  url : contextPath + "/comment/commentlike",
  data : "cid=" + cid,
  success : function(success) {
   if (success) {
    //
    $("#comment-agree-" + cid).bind("click",function(){
     commentLike(cid);
    });
    //不能写成:$("#comment-agree-" + cid).bind("click",commentLike(cid)); 不然会变成死循环了
    $('#like_' + cid).text(parseInt($('#like_' + cid).text()) + 1);
    //console.log(success);
   }/* else {
    $('#like_' + cid).text(parseInt($('#like_' + cid).text()) - 1);
    alert(friendTip);
   }*/
  }
 });
}



0 0