2013-06-18 78 views
0

我在我的网站上使用了Aloha编辑器。我已经能够成功地使用它,用户可以创建HTML并将其保存到我的数据库中。但是,我在将HTML内容预加载到编辑器中时遇到问题,以便用户可以编辑现有的HTML。将HTML内容加载到Aloha编辑器中

这里是JavaScript是初始化编辑:

<script type="text/javascript"> 
Aloha.ready(function() { 
    Aloha.jQuery('#richTextEditor').aloha(); 
    Aloha.bind('aloha-smart-content-changed', function (event, editable) { 
     $(".hiddenTextArea").val(Aloha.getActiveEditable().getContents()); 
     $("#btnSave").click(); 
    }); 
    var obj = ""; 
    Aloha.activeEditable().setContents($(".hiddenTextArea").val(), obj); 
}); 
</script> 

我曾尝试使用Aloha.activeEditable()setContents(),因为我看到这个在其他网站上提到,但我得到我的浏览器错误说

Error: TypeError: Aloha.activeEditable is not a function 

回答

0

阿罗哈!

这是

/** 
* Set the contents of this editable as a HTML string 
* @param content as html 
* @param return as object or html string 
* @return contents of the editable 
*/ 
Aloha.activeEditable.setContents('your html string', true); 

Aloha.activeEditable().setContents(); 

你见过这个例子:http://aloha-editor.org/howto-store-data-with-aloha-editor-ajax.php

你并不需要写的内容到一个文本。你也可以直接通过AJAX或者.aloha()直接提交它,如果你需要表单中其他值的内容(使用textarea时需要注意:如果textarea的html id是“xy”,那么id在Aloha编辑器中编辑将是“xy-aloha”)

你真的想要发送每个'aloha-smart-content-changed'事件的表单吗?也许使用'aloha-editable-deactivated'? 'aloha-smart-content-changed'在例如格式化为粗体时不会被触发...

+0

谢谢!是的,aloha-smart-content-changed事件适用于我,因为我只需要在用户单击“保存”按钮时保存。 –

0

Rene的答案是正确的,但我发现还有第二种方式预先填写Aloha编辑器的数据。您可以简单地将文本添加到您定位到Aloha编辑器的HTML元素(在我的情况下为#richTextEditor),然后再调用Aloha.bind()。