2

我使用 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插件。正如拉梅什所说,我使用了val()方法,但仍然(an empty string)在萤火虫控制台中返回我。这是我的代码:

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').sceditor('instance').val()并且效果很好。

4

4 回答 4

1

jQuery 对象是类似对象的数组,您可以innerHTML通过索引访问属性(查看控制台输出):

console.log($('iframe')[0].innerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="editor">
   <iframe>
      <!DOCTYPE html>
      <html>
         <head></head>
         <body>
            some important content here and I need to get them
         </body>
      </html>
   </iframe>
</div>

于 2015-05-08T05:30:20.010 回答
1

尝试 :

var contain = document.getElementById('iframe').contentWindow.document.body.innerHTML ;
// Use contain where need .

注意:仅当iframe源位于同一域中时才有效。

于 2015-05-08T05:36:16.170 回答
1

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

val() 自:1.3.5

获取编辑器的当前值。

这将从 WYSIWYG 编辑器返回过滤后的 HTML 或源编辑器的未过滤内容。

如果使用过滤 HTML 的插件,如 BBCode 插件,则在 BBCode 插件的情况下,这将返回过滤后的 HTML 或 BBCode。

句法

var val = instance.val();

返回类型:String

编辑器的过滤值

于 2015-05-08T06:41:52.400 回答
0

我希望这有帮助:

// Gives you the DOM element without the outside wrapper you want
$('.classSelector').html()

// Gives you the inside wrapper as well
$('.classSelector')[0].innerHTML
于 2015-05-08T06:03:03.997 回答