我用这种方式附加了span的点击事件。跨度位于表格内。
$("#tblImport").find("span").each(function () {
$(this).click(function () {
appendData(this);
});
});
此事件处理程序“appendData”发出 ajax 请求并获取数据。
function appendData(prntSpan) {
$(prntSpan).append(createElements());//ajax call.
updateEvents(prntSpan);
}
我想要的是分离这个点击事件处理程序并附加一个新的。 所以我尝试了这个。
function updateEvents(curntSpan) {
$(curntSpan).off('click');//this works
$(curntSpan).off('click', appendData);// but this does not work strange
$(curntSpan).on('click', toggleData(curntSpan));//nor a new click handler is attached
}
当上面使用 .on 的行被执行时,该函数被调用,但之后当您单击跨度时,不会触发任何事件。
最佳答案
.on()
函数检查中的语法错误 it 。所以我认为你应该更改你的代码:
$("#tblImport span").click(function (e) {
e.stopPropagation();
appendData(this);
});
这具有更清晰的语法。
function updateEvents(curntSpan) {
$(curntSpan).off('click');//this works
$("#tblImport").on('click', curntSpan, function() { toggleData(curntSpan); });
}
关于javascript - .on 和 .off 在 jquery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559113/