【问题标题】:Jquery Load returns [object object]Jquery 加载返回 [object 对象]
【发布时间】: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


【解决方案1】:

原来我只需要与对象的文本内容进行交互,所以下面的代码对我有用。

function testing(){

   let word = $('.summernote6').load('http://127.0.0.1:8887/js/summernote/cats.txt');
   let words = word.text();

    $('.summernote6').summernote('insertText', word.text());
    console.log(word.text())
}

【讨论】:

    【解决方案2】:

    load 方法在选择器中加载内容。

    你只能这样做:

    function testing(){
        $('.summernote6').load('http://127.0.0.1:8887/js/summernote/cats.txt');
    }
    

    它将在 .summernote6 元素中加载 cats.txt 的内容

    你得到一个对象,因为变量正在接收选择器,内容已经插入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-30
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2014-06-07
      • 1970-01-01
      相关资源
      最近更新 更多