繁体   English   中英

在wysiHTML5编辑器中以编程方式插入HTML

[英]Insert HTML programmatically in wysiHTML5 editor

我找到了javascript wysiwyg编辑器wysiHTML5

我正在尝试将元素<a href=...>添加到编辑器中,或者只是以编程方式启用粗体。

我的代码是:

var editor = new wysihtml5.Editor("textarea", {
    toolbar:      "toolbar",
    stylesheets:  "css/wysihtml5.css",
    parserRules:  wysihtml5ParserRules
});

editor.observe("load", function() {
    editor.composer.commands.exec("bold");
});

难道我做错了什么?

实际上没有,但你必须确保你的textarea(iframe)是专注的。 尝试使用on ,而不是observe 这是一个使用insertHTML为我工作的示例代码。

editor.on("load", function() {
  editor.focus();
  editor.composer.commands.exec("insertHTML","<a href=....>text</a>");
});

mateusmaso的解决方案给了我以下错误:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLDocument.execCommand]
[Break On This Error]   

Object.defineProperty(object, property, config);

所以我已经调查了一些,并找到了以下解决方案,其中(IMO)似乎更好:


var value = 'whatever you want to set';
// The jQuery reference to the textarea
var $ta = $('textarea#your-selector');
// The reference to the wysihtml5 editor - everything is contained within it 
var w5ref = $ta.data('wysihtml5');
// Check if you have it
if(w5ref){
   // if yes it's really an enhanced / wysihtml5ed textarea 
   // and use its setter
   w5ref.editor.setValue(value);
} else {
   // otherwise just simply populate the textarea as you normally would
   $ta.html(value);
}

资源

假设您之前使用$('#textarea-id').wysihtml5()实例化了编辑器$('#textarea-id').wysihtml5()

$('#textarea-id').data("wysihtml5").editor.setValue('new content');

字形

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM