jQuery CLEditor 动态添加文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5093076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CLEditor dynamic adding text
提问by moonwalker
I'm using CLEditorfor a website I'm working on. I'm trying to add dynamic text to the textarea with jQuery. Usually I'll be using something like:
我正在将CLEditor用于我正在处理的网站。我正在尝试使用 jQuery 向 textarea 添加动态文本。通常我会使用类似的东西:
$('#myText').val('Here some dynamic text');
But this just doesn't work with CLEditor. When disabling CLEditor it works fine however, enabling it again the text just disappears. I tried looking on the website for a solution, but I can't find any. Anyone had the same problem lately?
但这不适用于 CLEditor。然而,当禁用 CLEditor 时它工作正常,再次启用它文本就会消失。我尝试在网站上寻找解决方案,但找不到任何解决方案。最近有人遇到同样的问题吗?
Thanks in advance.
提前致谢。
采纳答案by Val
$("#input").cleditor({width:500, height:250,updateTextArea:function (){....}})
回答by verybadbug
CLEditor update iFrame content when blur method of textarea is called:
CLEditor 在调用 textarea 的 blur 方法时更新 iFrame 内容:
//make CLEditor
$(document).ready(function() {
$('#text').cleditor();
});
//set value
$('#text').val('new text data').blur();
回答by Francis Lewis
I was having the same issue and finally found something in the comments that was helpful, so here is what I have; hopefully this will come in handy to someone down the road.
我遇到了同样的问题,终于在评论中找到了一些有用的东西,所以这就是我所拥有的;希望这对以后的人有用。
// initializing the cleditor on document ready
var $editor = $('#description').cleditor()
Then when I get some html and need to dynamically insert it into the cleditor, here is what I use:
然后当我得到一些 html 并需要将它动态插入 cleditor 时,这是我使用的:
// update the text of the textarea
// just some simple html is used for testing purposes so you can see this working
$('#description').val('<strong>testing</strong>');
// update cleditor with the latest html contents
$editor.updateFrame();
回答by Artūras
For example if for someone it will be helpful, you can use it.
例如,如果对某人有帮助,您可以使用它。
Situation, i need on change dropdown field value, to change the content of cleditor.
情况,我需要更改下拉字段值,以更改 cleditor 的内容。
<script type="text/javascript">
$(document).ready(function() {
// Define and load CLEditor
$("#input").cleditor({
width: 800,
height: 300,
controls: "bold italic underline subscript superscript | color highlight removeformat | alignleft center alignright justify | table | undo redo | cut copy paste pastetext | print source ",
useCSS: false
});
// on change dropdown value
$('#dropdown').change(function() {
// doing AJAX request by GET
$.get(
// pushing AJAX request to this file
'ajax/change_dimmensions_table.php',
// pushing some data, from which to determine, what content I need to get back
{'dropdown_value':$('#dropdown').val()},
function(data, textStatus) {
// Clearing the content of CLEditor, adding new content to CLEditor
$("#input").cleditor({width:800, height:300, updateTextArea:function (){}})[0].clear().execCommand("inserthtml", data, null, null);
},
'html'
);
});
});
</script>
change_dimmensions_table.php:
change_dimensions_table.php:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
if ( ! empty( $_GET['dropdown_value']))
{
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
mysql_query("SET names utf8");
$sql = "
SELECT
`html_content`
FROM
`templates`
WHERE
`dropdown` = '{$_GET['dropdown_value']}'
LIMIT 1
";
$result = mysql_query( $sql) or die( mysql_error() . " SQL: {$sql}");
if ( mysql_num_rows($result) > 0)
{
while ( $row = mysql_fetch_object( $result))
{
$html = $row->html_content;
}
}
if ( ! empty( $html))
{
echo $html;
}
}
?>
回答by Tamas
I know it's kind of a late answer, but:
我知道这是一个迟到的答案,但是:
$('#myText').cleditor()[0].execCommand('inserthtml','Here some dynamic text');
is really the simplest answer, if you're asking me. The other simple answer was given by Francis Lewis above.
如果你问我,这真的是最简单的答案。上面的弗朗西斯·刘易斯给出了另一个简单的答案。
回答by user995789
execCommand is charm, but it does not work in IE, val().blur() is reliable
execCommand 很有魅力,但它在 IE 中不起作用,val().blur() 是可靠的