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

dae*_*mon 9 javascript wysiwyg wysihtml5

我找到了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");
});
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

mat*_*aso 15

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

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


Mat*_*yas 8

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);
}
Run Code Online (Sandbox Code Playgroud)

资源