简体   繁体   English

“未捕获的TypeError:无法读取未定义的属性'createDocumentFragment'”问题

[英]“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.

相关问题 未捕获的类型错误:无法读取未定义的属性“createDocumentFragment” - Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined 未捕获的类型错误:无法读取未定义的属性“createDocumentFragment”(使用所见即所得) - Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined (using wysiwyg) django 中的 Jquery:未捕获的类型错误:无法读取 null 的属性“createDocumentFragment” - Jquery in django : Uncaught TypeError: Cannot read property 'createDocumentFragment' of null 无法读取未定义的属性“ createDocumentFragment” - Cannot read property 'createDocumentFragment' of undefined “无法读取未定义的属性'createDocumentFragment'” - “Cannot read property 'createDocumentFragment' of undefined” 无法在重新格式化表格时读取未定义的属性&#39;createDocumentFragment&#39; - Cannot read property 'createDocumentFragment' of undefined on Table reformatting Javascript 问题 - 未捕获的类型错误:无法读取未定义的属性“incart” - Javascript issue- Uncaught TypeError: Cannot read property 'incart' of undefined Javascript问题-未捕获的TypeError:无法读取未定义的属性&#39;x&#39; - Javascript Issue - Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“ on” - Uncaught TypeError: Cannot read property 'on' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM