jQuery中给a标签加click事件,第一次点击时无响应,但之后就可正常操作。
<a id='share"+workId+"' class='share' onclick='share()' href='javascript:void(0);' studentId='"+student+"' workId='"+workId+"' good='"+good+"'>点我分享</a>
事件如下所示:
function share() {
$(".share").click(function () {
var url = contextPath+"/teacherWorkController/share.wm";
var data = {};
data.studentId=$(this).attr("studentId");
data.workId=$(this).attr("workId");
data.good="是";
var thiselm = $(this);
$.post(url,data,function (result) {
if(result.msg == "true"){
$("#img"+result.id).attr("src",contextPath+"/media/img/teacherwork/happy.jpg");
thiselm.text("取消分享");
thiselm.removeClass("share").addClass("cancel");
thiselm.unbind("click");
cancel(); //cancel()函数里边也会调用share()函数
}else {
alert("分享失败!");
}
},"json");
});
}
你把点击时间放在了一个函数里面,你把他放出来试试
同意楼上,你的share函数的作用就是放一个click事件,就没有存在的比要了。
window.onload = function(){
$(".share").click(function () {
var url = contextPath+"/teacherWorkController/share.wm";
var data = {};
data.studentId=$(this).attr("studentId");
data.workId=$(this).attr("workId");
data.good="是";
var thiselm = $(this);
$.post(url,data,function (result) {
if(result.msg == "true"){
$("#img"+result.id).attr("src",contextPath+"/media/img/teacherwork/happy.jpg");
thiselm.text("取消分享");
thiselm.removeClass("share").addClass("cancel");
thiselm.unbind("click");
cancel();
}else {
alert("分享失败!");
}
},"json");
});
}
jquery中这么用:
$(function(){
$(".share").click(function(){
//...
});
})
补充补充~~~~~我之所以写成
function share() {
$(".share").click(function () {
cancel(); //cancel()函数里边也会调用share()函数
}
function cancel() {
$(".cancel").click(function () {
share(); //cancel()函数里边也会调用share()函数
}
这两个函数是要互相调用的,另:share()和cancel()我在$(function()){
}里都提前定义过得
你试试这样
var share=function(){
};
var cancle=function(){
};
$(".cancle").on("click",share);
$("share").on("click",cancle);
function share() {
$(".share").unbind("click").click(function () {
}
}