【发布时间】:2011-06-16 05:53:44
【问题描述】:
我正在尝试动态生成 flexigrid 生成表的单元格的 onclick 事件处理程序:
// ...
preProcess: function (data) {
var rows = data.rows;
for (var i = 0; i < rows.length; ++i) {
var row = rows[i];
// If and only if this condition is true, then
// row.cell[0] must be converted into a hyperlink.
if (row.cell[1] != '0') {
// I don't want to use the href attribute, because that would
// force me to define a non-anonymous function.
row.cell[0] = '<a href="javascript:void(0)" id="E'
+ i + '">' + row.cell[0] + '</a>';
// So I'm going to try assigning the onclick attribute.
$('#E' + i).click(function () {
window.open('doc.php?q=' + this.id, 'D' + this.id,
'menubar=0,toolbar=0,directories=0,location=0,status=0,' +
'resizable=0,scrollbars=0,width=600,height=300');
});
$('#E' + i).click().id = row.cell[4];
}
}
return data;
}
// ...
但是,当我单击生成的超链接时,它们不起作用。有什么问题?我使用闭包? <a> 标签不接受onclick 属性?
注意:自从我开始使用 jQuery,我的策略是所有函数都应该是匿名函数。请不要建议我使用普通函数。
【问题讨论】:
-
出于好奇,为什么必须'所有函数......都是匿名函数'?无意批评,我只是真诚地好奇。
-
我想推动自己学习和掌握EVERY的概念。
-
“所有函数都应该是匿名函数” 我采取了类似的策略——所有的锤子都应该是蓝色的。不要建议我使用红锤。
-
如果您想学习和掌握每一个概念,请从使用正确工具的概念开始:)
-
jQuery 没有规定匿名函数的使用,当然也不是唯一的。如果您想使用
.unbind(),您可能会遇到麻烦。对我来说,匿名函数的独占使用听起来像是等待发生的内存泄漏。
标签: javascript jquery html closures flexigrid