【发布时间】:2020-04-03 04:14:52
【问题描述】:
我正在使用 Jquery 从文本文件中加载带有文本的 Summernote 模块,但是当我执行我的函数时,它会将负载识别为对象,而不是在文档中显示文本。
我怎样才能让 jquery 返回文件内的文本而不是文件作为对象。
function testing(){
let word = $('.summernote6').load('http://127.0.0.1:8887/js/summernote/cats.txt');
$('.summernote6').summernote('insertText', word);
console.log(word)
}
然后控制台显示[object object]
【问题讨论】:
-
jQuery 的
.load()函数返回您调用load的jQuery 对象,即$('.summernote6')对象。远程资源的内容在加载后插入到该元素中 -
阅读文档。
.load()不返回加载的内容,它返回一个 jQuery 集合,以便您可以链接它,就像许多其他 jQuery 方法一样。.load()所做的是将集合中所有元素的所有内容替换为加载的内容。这似乎不是您想要的,而是您想将其存储在变量中。为此,您应该使用.ajax()。 -
我想你其实想要
$.get('http://127.0.0.1:8887/js/summernote/cats.txt').done(word => console.log(word))
标签: javascript jquery summernote