【发布时间】:2021-04-01 05:45:43
【问题描述】:
与 Axios 通话后,我想用新的项目替换当前项目。我是这样进行的:
var replacedElement = "<span class='float-right' aria-hidden='true'>" +
"<i class='fas fa-check icon-color'></i>" +
"</span>";
axios.post(url).then(function (response) {
$(this).replaceWith($.parseHTML(replacedElement));
});
但我有以下错误:Uncaught (in promise) TypeError: Cannot read property 'createDocumentFragment' of undefined
我的$(this) 引用了span 元素:
所以我不明白为什么会出现这个错误
【问题讨论】:
-
您是否在
then函数中添加了console.log(this),因为我很确定this不会引用同一个对象?如果这是问题,请尝试将this存储在var self = this之类的变量中,并在then函数中使用self而不是this。 -
你是对的。我做
console.log ($(this))的时候,是在调用axios之前做的。做得好,谢谢! :) -
我添加了一个正确的答案,其中包含几个解决方案和一个链接,用于了解如何计算
this。
标签: javascript jquery dom replace replacewith