[英]“Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined” issue
function addEditButton() {
$(".editButton").remove();
$(".transaction.highlight").removeClass('highlight');
$(this).addClass('highlight');
$(this).append("<input type='button' class='editButton' value='edit' />")
}
$("body").on('click', '.transaction', addEditButton());
I am getting an error message of "Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined" when using the above code. 我在使用上面的代码时收到“Uncaught TypeError:无法读取未定义的属性'createDocumentFragment'的错误消息”。
I've tried searching through some of the same questions that have been posted about this and I now assume the error is because of the way I am using "this" but I am not sure how to fix the problem. 我已经尝试搜索已发布的有关此问题的一些相同问题,我现在假设错误是因为我使用“this”的方式,但我不知道如何解决问题。
The code works just fine if I put all the code inside the $("body").on call, but not when I use the external addEditButton() function. 如果我把所有代码都放在$(“body”)中,那么代码就可以正常工作了。但是当我使用外部addEditButton()函数时却没有。 Can anyone help please? 有人可以帮忙吗?
绑定时不要调用该函数,只需将其作为参考传递即可。
$("body").on('click', '.transaction', addEditButton);
You're assigning the result returned by your addEditButton function, instead of a reference to the function itself. 您将分配addEditButton函数返回的结果,而不是对函数本身的引用。 Drop the parentheses and it should work: 删除括号,它应该工作:
$("body").on('click', '.transaction', addEditButton);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.