jQuery:修改iframe内容



据我所知,这段代码应该可以工作,但它没有,我完全被难住了。任何想法吗?

jQuery

$('.article_chooser').click(function() {
    var src = $(this).attr('value');
$("#doc_edits_attributes_0_body").parent("iframe html body").html($("#article"+src).html());
console.log($("#doc_edits_attributes_0_body").parent("iframe html body").html());
});

<textarea class="editable_areas" cols="40" id="doc_edits_attributes_0_body" name="doc[edits_attributes][0][body]" rows="20" style="display: none;"></textarea>
<iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0">
    <html>
        <body marginwidth="0" marginheight="0" contenteditable="true" class="editable_areas wysihtml5-editor" spellcheck="true">
        </body>
    </html>
</iframe>

干杯!

要编辑iframe的内容,必须调用iframe内的body标签:

<iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0" id="iframe">
    <html>
        <body marginwidth="0" marginheight="0" contenteditable="true" class="editable_areas wysihtml5-editor" spellcheck="true">
        </body>
    </html>
</iframe>
<script type="text/javascript">
$(document).ready(function() {
    $('#iframe').contents().find('body').html('New Content');
});
</script>

wysihtml5插件有API,可以使用getValue方法:

var value = editorInstance.getValue();

或者如果您想设置一个值,您可以使用setValue方法:

editorInstance.setValue('a string');

或者如果你想附加html内容,你可以使用.exec()方法:

editorInstance.composer.commands.exec("insertHTML", "<p>bar</p>");

最新更新