使用WYSIHTML5和jQuery将文本附加到文本区域

时间:2012-11-02 00:09:17

标签: jquery wysihtml5

我正在使用最新的(0.3.0)版本的WSYIHTML5作为留言板。该板的一个特点是报价选项。我一直试图弄清楚如何将引用附加到文本区域。

我有以下jQuery作为测试

$('.quote').click(function(){
  $('textarea').append('test this');
  alert($('textarea').text());
});

警告显示文本已附加到隐藏的textarea,但它不会在WYSIHTML5视图的iframe中更新。

奇怪的是,如果我在初始化WYSIHTML5后直接附加文本,它会很好地附加。

关于让它发挥作用的任何想法?

3 个答案:

答案 0 :(得分:12)

因此,为了更新iframe并使用HTML格式化任何文本,我执行了以下操作..

var value = editor.getValue();
var text = '<b>some text</b>;'
editor.setValue(value + text, true);

这是假设WYSIHTML5在变量'editor'下实例化。它会附加引用,并正确格式化文本。如果您希望通过WYSIHTML5解析器规则运行该值,则布尔值为。

如果有人知道更好的方法,我会全力以赴。

答案 1 :(得分:1)

$('iframe').contents().find('.wysihtml5-editor').html('your new content  here');

答案 2 :(得分:0)

对于bootstrap-wysihtml5使用以下函数:

<script>

  $(function () {
    // Replace the <textarea id="editor1"> with a CKEditor
    // instance, using default configuration.
  //  CKEDITOR.replace('editor1');
    //bootstrap WYSIHTML5 - text editor
    $(".textarea").wysihtml5();

  });



function injectString(str){
   var sel=frames[0].getSelection();
   var nd=sel.anchorNode;
   var txt=nd.data+'';
   var pos=sel.anchorOffset||0;
   sel.anchorNode.data=txt.slice(0,pos)+str+txt.slice(pos);
 }</script>

像这样使用

<button id="name" data-wysihtml5-command="foreFrac" onclick="injectString('{name}')" type="button" class="btn btn-block btn-success btn-lg">Name </button>
相关问题