将tinyMCE设置应用到动态创建的文本区域 [英] Apply tinyMCE settings to dynamicly created textarea
问题描述
< script type =text / javascriptsrc ={{ STATIC_PREFIX}} js / tiny_mce / tiny_mce.js
< / script>
< script type =text / javascriptsrc ={{STATIC_PREFIX}} js / tiny_mce / tiny_init.js
< / script>
< script type =text / javascript>
tinyMCE.settings = configArray [1];
tinyMCE.execCommand('mceAddControl',true,tobs);
< / script>
{%obs%obs%}
< div id =obs>
DateTime:< samp> {{obs.date}}< / samp>< br>
说明:< br>
< textarea id =tobsclass =ro> {{obs.description}}< / textarea>< br>
{%for obscontent%}
文件:
< a href =观察/ {{f}} title =下载文件>
< script>
get_name({{f}})
< / script>
< / a>< br>
{%endfor%}
作者:< samp> {{obs.user}}< / samp>< br>
类型:< samp> {{obs.category}}< / samp>< br>< br>
< / div>
{%empty%}
< br>对不起,DataBase中没有任何意见。
{%endfor%}
但问题是只有第一个textarea正在从我的设置configArray [1],看起来像tinymce,第二个和第三个只是简单的textarea没有tinymce设置。
我该怎么改?
更改每个textarea的ID。 ids应该在HTML中是唯一的,但是你的for循环正在创建具有相同ID的多个文本区域。 TinyMCE正在尝试使用id =tobs来渲染textarea,并在找到多个文本时感到困惑。您可以尝试:
{%obs%obs%}
...
{%with tobs| add:forloop.counter as area_id%}
< textarea id = {{area_id}} class =ro> {{obs.description}}< / textarea< br>
{%endwith%}
...
{%endfor%}
这应该使您的textareas与ids:tobs1,tobs2,tobs3,...,这应该解决问题。
阅读更多在这里: https://docs.djangoproject.com/en/dev/ref/templates/ builtins /#for
但是应该有一个更优雅的解决方案来解决这个问题。到目前为止,我已经能够找到这个例子,它使用类来区分文本区域,并且根本不使用ids,这与上一个链接的解决方案相矛盾: http://www.tinymce.com/tryit/3_x/multiple_configs.php
I create lots of tinymce with django:
<script type="text/javascript" src="{{ STATIC_PREFIX }}js/tiny_mce/tiny_mce.js"
</script>
<script type="text/javascript" src="{{ STATIC_PREFIX }}js/tiny_mce/tiny_init.js"
</script>
<script type="text/javascript">
tinyMCE.settings = configArray[1];
tinyMCE.execCommand('mceAddControl', true, "tobs");
</script>
{% for obs in obss %}
<div id="obs">
DateTime:<samp>{{ obs.date }}</samp><br>
Description: <br>
<textarea id="tobs" class="ro">{{ obs.description }}</textarea><br>
{% for f in obs.content %}
File:
<a href=Observations/{{f}} title="download file">
<script>
get_name("{{f}}")
</script>
</a><br>
{% endfor %}
Author:<samp>{{ obs.user }}</samp><br>
Type:<samp>{{ obs.category }}</samp><br><br>
</div>
{% empty %}
<br>Sorry, no observations in DataBase.
{% endfor %}
But problem is thet only first textarea is getting my settings from configArray[1] and looks like tinymce, second and third are just simple textarea with no tinymce settings.
How can I change whis?
Change the id for each textarea. ids are supposed to be unique in HTML, but your for loop is creating multiple textareas with the same id. TinyMCE is trying to render the textarea with id="tobs" and gets confused when it finds more than one. You could try:
{% for obs in obss %}
...
{% with "tobs"|add:forloop.counter as area_id %}
<textarea id={{ area_id }} class="ro">{{ obs.description }}</textarea<br>
{% endwith %}
...
{% endfor %}
That should make your textareas with ids: tobs1, tobs2, tobs3,..., which should fix the problem.
Read more here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
And here: TinyMCE not working when loading two textareas
But there should be a more elegant solution to this problem. So far I've been able to find this example, which uses class to distinguish between textareas and doesn't use ids at all, which contradicts the solution from the last link: http://www.tinymce.com/tryit/3_x/multiple_configs.php
这篇关于将tinyMCE设置应用到动态创建的文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!