javascript - 获取 iframe 内容作为字符串

标签 javascript jquery iframe rich-text-editor sceditor

我使用 jquery 插件来生成富文本编辑器。该编辑器生成如下结构:

<div id="editor" ... >
   ...
   <iframe>
      <!DOCTYPE html>
      <html>
         <head>...</head>
         <body>
            //some important content here and I need to get them
         </body>
      </html>
   </iframe>
</div>

现在,我想将 iframe 体内的所有内容作为 String 获取。我测试了 $("#editor").contents().find("body"),但这返回了一个对象,而不是字符串。还尝试了 $("#editor").contents().find("body").outerHTML,但这返回我 undefined。我怎样才能做到这一点?请帮我。感谢您抽出时间。

编辑:

我使用SCEditor插入。正如 Ramesh 所说,我使用了 val() 方法,但仍然在 firebug 控制台中返回我(空字符串)。这是我的代码:

var instance = $("textarea").sceditor({
        plugins: "bbcode",
        style: "../../editor/minified/jquery.sceditor.default.min.css",
        //some othe options
    });
    $("#save").click(function(){
        console.log(instance.val());
    });

正如 Ramesh 所建议的,我使用了 $('textarea').seditor('instance').val() 并且它有效。

最佳答案

在SCEditor中要获取富文本框的值,您必须使用.val方法

val() Since: 1.3.5

Gets the current value of the editor.

This will return the filtered HTML from the WYSIWYG editor or the unfiltered contents of the source editor.

If using a plugin that filters the HTML like the BBCode plugin, this will return the filtered HTML or BBCode in the case of the BBCode plugin.

Syntax

var val = instance.val();

Return Type: String

The filtered value of the editor

关于javascript - 获取 iframe 内容作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116410/

相关文章:

javascript - 在不更改 $scope 的情况下强制重新编译过滤后的 ng-attr

javascript - 如何仅在 Firefox 上显示/隐藏 jquery 脚本

javascript - 追加一个元素作为父元素

javascript - 如何在 javascript 中创建一个隐藏的 iframe 并将其源代码放入文本框和作为变量?

javascript - 重新载入谷歌地图

javascript - 在Electron App中,app.quit()&&.exit(0)无法终止进程,如何处理?

javascript - .addClass Javascript 代码未正确执行 - WordPress 网站

javascript - 一页上有多个 jcarousel 问题

javascript - JS 将 TARGET 添加到表单

javascript - 将结果打印到我的 Select 语句到我的 HTML(phonegap) 中的列表中