Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在wysiHTML5编辑器中以编程方式插入HTML_Javascript_Wysiwyg_Wysihtml5 - Fatal编程技术网

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

Javascript 在wysiHTML5编辑器中以编程方式插入HTML,javascript,wysiwyg,wysihtml5,Javascript,Wysiwyg,Wysihtml5,我找到了javascript所见即所得编辑器 我正在尝试将元素添加到编辑器中,或者只是以编程方式启用粗体 我的代码是: var editor = new wysihtml5.Editor("textarea", { toolbar: "toolbar", stylesheets: "css/wysihtml5.css", parserRules: wysihtml5ParserRules }); editor.observe("load", functio

我找到了javascript所见即所得编辑器

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

我的代码是:

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

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

我做错什么了吗?

事实上没有,但是你必须确保你的文本区域(iframe)是聚焦的。尝试在上使用
,而不是
观察
。下面是一个使用insertHTML为我工作的示例代码

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

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

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLDocument.execCommand] [Break On This Error] Object.defineProperty(object, property, config);

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


谢谢!我花了几个小时试图弄明白这一点,它真的应该在文档中如果你不介意在编辑器中删除html,你可以使用setValue命令,即editor.setValue(“”);请注意脚本末尾的冒号。应该是分号吗?加上分号后,我急于阅读评论,花了半个小时才注意到这对我来说非常有效。但是,在插入“insertHTML”之后,如何模糊/取消对编辑器的聚焦?我尝试了一些方法,其中包括
editor.blur()
,但似乎没有任何效果。感谢您提供这个优雅的解决方案

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').data("wysihtml5").editor.setValue('new content');