JavaScript中带有动态参数的动态onclick函数赋值?
我有以下代码:JavaScript中带有动态参数的动态onclick函数赋值?,javascript,html,Javascript,Html,我有以下代码: document.getElementById('img'+i).onclick = function(){popup_show('popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, 'AddEditSchedule;;popup_drag2;;EditSched;;'+String(array_msg_id[3])+';;view', 'popup_dr
document.getElementById('img'+i).onclick = function(){popup_show('popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, 'AddEditSchedule;;popup_drag2;;EditSched;;'+String(array_msg_id[3])+';;view', 'popup_drag', 'popup_exit', 'screen-center', 0, 0);};
…但是当我单击图像时,array\u msg[I]
的数据是循环的最后一个数据,这意味着索引是循环的长度。我用IE来做这个
请告诉我怎么做。在FF中,它工作正常,因为我使用了setAttribute
@博宾斯
document.getElementById('img'+i).onclick= popup_show.bind(window, 'popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict,'AddEditSchedule;;popup_drag2;;EditSched;;'+array_msg_id[3]+';;view','popup_drag', 'popup_exit', 'screen-center', 0, 0 );
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
var args= Array.prototype.slice.call(arguments, 1);
return function() {
return that.apply(owner,
args.length===0? arguments : arguments.length===0? args :
args.concat(Array.prototype.slice.call(arguments, 0))
);
};
};
}
你需要使用一种新的方法。如果您提供了循环代码以及在循环中执行的代码,这会有所帮助,但是假设您有一个标准的for
循环遍历数组,那么以下代码应该可以工作:
for (var i = 0, l = array.length; i < l; i++)
{
(function(i) {
document.getElementById("img" + i).addEventListener("click", function() {
popup_show("popup", array_msg[i] + "|||" + date("Y-m-d", strtotime(lec_date)) + "-==-" + str_view_forConflict, "AddEditSchedule;;popup_drag2;;EditSched;;" + String(array_msg_id[3]) + ";;view", "popup_drag", "popup_exit", "screen-center", 0, 0);
}, false);
})(i);
}
for(var i=0,l=array.length;i
另外,您不应该在Firefox中使用setAttribute
。相反,最好使用or,它允许您在事件触发时添加多个要调用的函数,从而与其他代码配合使用(如果两位代码以element.onclick=function().我在上面的代码示例中使用了元素.addEventListener
。您需要使用。如果您提供循环代码以及在循环中执行的代码,这会有所帮助,但是假设您有一个标准的用于遍历数组的循环,以下代码应该可以工作:
for (var i = 0, l = array.length; i < l; i++)
{
(function(i) {
document.getElementById("img" + i).addEventListener("click", function() {
popup_show("popup", array_msg[i] + "|||" + date("Y-m-d", strtotime(lec_date)) + "-==-" + str_view_forConflict, "AddEditSchedule;;popup_drag2;;EditSched;;" + String(array_msg_id[3]) + ";;view", "popup_drag", "popup_exit", "screen-center", 0, 0);
}, false);
})(i);
}
for(var i=0,l=array.length;i
另外,您不应该在Firefox中使用setAttribute
。相反,最好使用or,它允许您在事件触发时添加多个要调用的函数,因此这可以很好地与其他代码配合使用(如果两位代码将一个函数分配给element.onclick=function()形式的单击事件){…
,则第二个赋值覆盖第一个赋值(不好).我在上面的代码示例中使用了元素.addEventListener
。您遇到了循环变量闭包问题。这是C风格语言中使用闭包的常见问题,如JavaScript和Python。有关在第二个闭包中绑定循环变量的解决方案,请参阅的公认答案
稍微少一点嵌套的解决方案是使用function.bind()
:
for(var i=0;i您遇到了循环变量闭包问题。这是C风格语言中使用闭包的常见问题,如JavaScript和Python。有关在第二个闭包中绑定循环变量的解决方案,请参阅的公认答案
稍微少一点嵌套的解决方案是使用function.bind()
:
for(var i=0;i闭包。您需要使用JavaScript闭包。查看是否回答帮助。闭包。您需要使用JavaScript闭包。查看是否回答帮助。工作答案:
var closures = [];
for (var i = 0; i < array.length; i++){
closures[i] = (function(tmp) {
return function() {
document.getElementById(tmp + '05').onclick = function(){popup_show("popup", array_msg[tmp]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, "AddEditSchedule;;popup_drag2;;EditSched;;"+ String(array_msg_id[3]) +";;view", "popup_drag", "popup_exit", "screen-center", 0, 0)};
};
})(i);
closures[i]();
}
var闭包=[];
对于(var i=0;i
感谢您的回答。我有了一个想法来概括它工作答案:
var closures = [];
for (var i = 0; i < array.length; i++){
closures[i] = (function(tmp) {
return function() {
document.getElementById(tmp + '05').onclick = function(){popup_show("popup", array_msg[tmp]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, "AddEditSchedule;;popup_drag2;;EditSched;;"+ String(array_msg_id[3]) +";;view", "popup_drag", "popup_exit", "screen-center", 0, 0)};
};
})(i);
closures[i]();
}
var闭包=[];
对于(var i=0;i
感谢您的回答。我有了一个想法来概括它Treby,这是您答案的清理版本。您添加的额外匿名功能不是必需的
for (var i = 0, l = array.length; i < l; i++ ) {
document.getElementById(i + '05').onclick = (function(tmp) {
return function() {
popup_show(
"popup",
array_msg[tmp] + '|||' + date('Y-m-d', strtotime(lec_date)) + '-==-' + str_view_forConflict,
"AddEditSchedule;;popup_drag2;;EditSched;;" + String(array_msg_id[3]) + ";;view",
"popup_drag", "popup_exit", "screen-center", 0, 0
);
};
})(i);
}
for(var i=0,l=array.length;i
编辑以修复关闭问题Treby,这是您答案的清理版本。您添加的附加匿名功能不是必需的
for (var i = 0, l = array.length; i < l; i++ ) {
document.getElementById(i + '05').onclick = (function(tmp) {
return function() {
popup_show(
"popup",
array_msg[tmp] + '|||' + date('Y-m-d', strtotime(lec_date)) + '-==-' + str_view_forConflict,
"AddEditSchedule;;popup_drag2;;EditSched;;" + String(array_msg_id[3]) + ";;view",
"popup_drag", "popup_exit", "screen-center", 0, 0
);
};
})(i);
}
for(var i=0,l=array.length;i
编辑以修复关闭问题那么您建议使用什么?