javascript - "Uncaught TypeError: Cannot read property ' createDocumentFragment ' of undefined"问题

标签 javascript jquery

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());

使用上述代码时,我收到“未捕获的类型错误:无法读取未定义的属性‘createDocumentFragment’”的错误消息。

我已经尝试搜索一些与此相关的相同问题,现在我假设错误是因为我使用“this”的方式,但我不确定如何解决这个问题。

如果我将所有代码都放在 $("body").on 调用中,代码就可以正常工作,但是当我使用外部 addEditButton() 函数时就不行了。有人可以帮忙吗?

最佳答案

绑定(bind)时不要调用该函数,只需将其作为引用传递即可。

$("body").on('click', '.transaction', addEditButton);

关于javascript - "Uncaught TypeError: Cannot read property ' createDocumentFragment ' of undefined"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889602/

相关文章:

javascript - EXTJS + startEditByPosition 函数在移动到其他行时不会引发焦点事件

javascript - 如何在新用户的 Google 一键式 UI 中显示隐私政策 URL 和服务条款 URL?

javascript - 如何在浏览器窗口调整大小时自动调整 iframe 内容大小?

javascript - 单击某个元素时如何显示接下来的 3、6、9 等子元素?

jquery - 使用 jQuery 收集所有没有特定类的 "visible"表单元素的最佳方法?

javascript - 如何使用 div 标签的类获取表单数据?

javascript - 从 URL 中搜索并删除带有可选字符的参数

javascript - LoDash - 按数组属性分组而不将值作为一个键

jQuery 将 target 属性添加到变量中的许多 href 元素

JavaScript:根据不同的对象数组编译对象数组