2

有没有办法使用 jquery 将文本插入到 bootstrap wysiwyg 中?顺便说一句,我从这个页面使用了 bootstrap-wysihtml5 http://jhollingworth.github.io/bootstrap-wysihtml5/ 文本实际上是来自下拉列表的模板,然后应该在 wysisyg 上插入所选的 onchange 值。

我尝试了这段代码,但它只将文本插入隐藏的文本区域而不是所见即所得

$(document).ready(function(e) {
    $("#template").change(function(){
        var template = $("#template").val();
        if(template != '') {
            $('.textarea, .wysihtml5-sandbox').append(template);
        }
    });
});
4

1 回答 1

4

您不能简单地textarea从包含页面的 DOM 访问 ,因为实际的所见即所得编辑器位于带有.wysihtml5-sandbox. 因此,要访问 iFrame 的 DOM 并对其进行修改,我们必须首先访问 iFrame DOM:

  $('.wysihtml5-sandbox').contents() 
  //Now we have the iFrame DOM to apply selectors to

  $('.textarea', $('.wysihtml5-sandbox').contents())
  //Now we have access to the textarea which contains the editor's current text

  $('.textarea', $('.wysihtml5-sandbox').contents()).append('Hello World!')
  //Now we have appended content to the editor view, the original goal 

额外的东西:

$('.textarea', $('.wysihtml5-sandbox').contents()).removeClass('placeholder')
//Stops the text from being formatted as a placeholder (i.e. grayed out)
$('.textarea', $('.wysihtml5-sandbox').contents()).empty()
//Remove the existing placeholder text by emptying the textarea content
于 2013-09-05T16:34:37.057 回答