JS循环中使用bind函数的参数传递问题

时间:2021-06-10 02:44:30
JS循环中使用bind函数的参数传递问题,问题代码如下:
for (var sc in result) {
var tempp = '<div class="sidebar_todo_temp">' +
'<img src="grpcd/common/img/close_s.png">' +
'<p class="senderman">' +
result[sc].senderuser +
'</p>' +
'<p class="vtitle">' +
result[sc].title +
'</p>' +
'<p class="senddate">' +
fommatDate(result[sc].sendtime) +
'</p>' +
'</div>';
var pp = $(tempp);
pp.hover(
function() {
$(this).find('p').css('background-color', '#2C2424');
},
function() {
$(this).find('p').css('background-color', '#666666');
}
); $("#sidebar_todo_area").append(pp);
pp.bind('click', function() {
var opt = {
num: 400,
title: "页签",
imgsrc: "",
url: "workflow/towfdeal.do?recordid=" + result[sc].recordid + "&nid=" + result[sc].nid + "&checkflowid=" + result[sc].id + "&inforid=" + result[sc].inforid+"&taskid=" + result[sc].taskid,
width: 300,
height: 300,
circleurl: "",
preloading: 0
};
Core.createPageNC(opt);
});
}

传入的参数是循环的最后一次构造的,改造代码如下:

  for (var sc in result) {
var tempp = '<div class="sidebar_todo_temp">' +
'<img src="grpcd/common/img/close_s.png">' +
'<p class="senderman">' +
result[sc].senderuser +
'</p>' +
'<p class="vtitle">' +
result[sc].title +
'</p>' +
'<p class="senddate">' +
fommatDate(result[sc].sendtime) +
'</p>' +
'</div>';
var pp = $(tempp);
pp.hover(
function() {
$(this).find('p').css('background-color', '#2C2424');
},
function() {
$(this).find('p').css('background-color', '#666666');
}
);
var urll="workflow/towfdeal.do?recordid=" + result[sc].recordid + "&nid=" + result[sc].nid + "&checkflowid=" + result[sc].id + "&inforid=" + result[sc].inforid+"&taskid=" + result[sc].taskid;
var numm=result[sc].taskid;
alert(numm);
var opt = {
num: numm,
title: "页签",
imgsrc: "",
url: urll,
width: 300,
height: 300,
circleurl: "",
preloading: 0
};
$("#sidebar_todo_area").append(pp);
pp.bind('click', opt,function(e) {
Core.createPageNC(e.data);
});
}