jquery怎么移除不了事件呢?
admin 发表于 2010-07-29 | 来源:互联网 | 阅读:
- JScript code
-
function RecommandProduct(obj, id) { setRecommand(obj, id, 1, true); } function UnRecommandProduct(obj, id) { setRecommand(obj, id, 0, false); } function setRecommand(obj, id, state, up) { var _this = $(obj); var recommandCountVal = parseInt(recommandCount.html()); if(!up||(recommandCountVal+1)<=MaxRecommend) { $.ajax({ type: "POST", dataType: "json", url: "/Manage/Mall/setRecommand.aspx", data: { state: state, productId: id, __RequestVerificationToken: $("[name='__RequestVerificationToken']").val() }, success: function(data) { if (data.state == 1) { $("#" + id + "").attr("recommand", state); if(up) { recommandCount.html(recommandCountVal+1); _this.css('color',"red").html("取消橱窗推荐").unbind( "click" ).bind("click",function(e){ UnRecommandProduct(e.target,id); }); } else { recommandCount.html(recommandCountVal-1); _this.css("color","#0000FF").html("设为橱窗推荐").unbind( "click" ).bind("click",function(e){ RecommandProduct(e.target,id); }); } } else if (data.state == 3) alert("最多只能推荐50个圣品"); else alert('对不起,系统繁忙,请稍候再尝试'); } }); } else { if(up) alert('最多只能推荐'+MaxRecommend+'个圣品'); } } //下面是元素 <a style="color:red" onclick='UnRecommandProduct(this,<%=ele.ProductId %>)' href='javascript:void(0);'>取消橱窗推荐</a> <a style="color: #0000FF" onclick='RecommandProduct(this,<%=ele.ProductId %>)' href='javascript:void(0);'> 设为橱窗推荐</a>
一开始我就设定了初始值,我想每次点击之后都变换一下他的 click 函数。。
请高手赐教

我生气了,决定把整个元素移除掉。。。换成这样JScript code
function setRecommand(obj, id, state, up) {
var _this = $(obj);
var recommandCountVal = parseInt(recommandCount.html());
if(!up||(recommandCountVal+1)<=MaxRecommend)
{
$.ajax({
type: "POST",
dataType: "json",
url: "/Manage/Mall/setRecommand.aspx",
data: { state: state, productId: id, __RequestVerificationToken: $("[name='__RequestVerificationToken']").val() },
success: function(data) {
if (data.state == 1) {
$("#" + id + "").attr("recommand", state);
if(up)
{
recommandCount.html(recommandCountVal+1);
_this.after("<a style=’color:red’ onclick=’UnRecommandProduct(this,"+id+")’ href=’http://topic.csdn.net/u/20100727/16/javascript:void(0);’>取消橱窗推荐</a>").remove();
}
else
{
recommandCount.html(recommandCountVal-1);
_this.after("<a style=’color:#0000FF’ onclick=’RecommandProduct(this,"+id+")’ href=’http://topic.csdn.net/u/20100727/16/javascript:void(0);’>设为橱窗推荐</a>").remove();
}
}
else if (data.state == 3)
alert("最多只能推荐50个圣品");
else
alert(‘对不起,系统繁忙,请稍候再尝试’);
}
});
}
else
{
if(up)
alert(‘最多只能推荐’+MaxRecommend+’个圣品’);
}
}
变换一下他的 click 函数 代表什么意思?
从他的RecommandProduct 变为 UnRecommandProduct意思是,原来 要移除原来的事件函数,再绑定另外的函数
从他的RecommandProduct 变为 UnRecommandProduct意思是,移除原来的事件函数,再绑定另外的函数
从他的RecommandProduct 变为 UnRecommandProduct意思是,移除原来的事件函数,再绑定另外的函数CSS code
$(‘div’).bind(‘click’, RecommandProduct);//为div绑定RecommandProduct 函数
$(‘div’).unbind(‘click’, RecommandProduct);//取消RecommandProduct 函数
$(‘div’).bind(‘click’, UnRecommandProduct);//为div绑定UnRecommandProduct函数
谢谢 5 楼的,但是我一开始 就像你说那样操作,下面是代码 _this.css(‘color’,"red").html("取消橱窗推荐").unbind( "click" ).bind("click",function(e){ UnRecommandProduct(e.target,id); });我原来就是这个样子的。但是我一开始 显示的时候 就已经将函数写到标签里去,而没用bind()<a style="color: #0000FF" onclick=’RecommandProduct(this,<%=ele.ProductId %>)’ href=’http://topic.csdn.net/u/20100727/16/javascript:void(0);’>设为橱窗推荐</a>
CSS code
<a style="color: #0000FF" onclick=’RecommandProduct(this,<%=ele.ProductId %>)’ href=’http://topic.csdn.net/u/20100727/16/javascript:void(0);’>设为橱窗推荐</a>
不要用代码绑定,用jquery的函数
如果是代码绑定的,就用$("a").removeAttr("onclick").attr("onclick", "UnRecommandProduct(this,<%=ele.ProductId %>)");
呵呵。。我就知道要这样做的啦。虽然没经过测试,但是是可以的。谢谢啦,是不是用代码绑定 是不能用 unbind 的?
呵呵。。我就知道要这样做的啦。虽然没经过测试,但是是可以的。谢谢啦,是不是用代码绑定 是不能用 unbind 的?unbind 是取消绑定$(‘div’).bind(‘click’, RecommandProduct);//为div绑定RecommandProduct 函数$(‘div’).unbind(‘click’, RecommandProduct);//取消RecommandProduct 函数
谢谢赐教,其实我都知道这个怎样用。但我将代码写到标签里去,他好像移除不了,明天结贴。谢谢帮忙
写到标签里面,被视为属性,要用removeAttr