我在 laravel 中试用 CKeditor,我在将 html 插入编辑器时遇到了问题。添加编辑器后,我想设置值,以便您可以编辑已经存在的值。
HTML:
@if (!empty($proposal->reference_sites))
<div class="form-group row mb-4">
<label for="reference_sites" class="col-sm-3 col-form-label form-control-lg">Reference sites:</label>
<div class="col-sm-10" id="reference_sites"></div>
</div>
@endif
JS:
if($('#reference_sites').length){
$output = "<textarea id='ckeditor-rs' name='ckeditor-rs' rows='10' cols='80'></textarea>";
$('#reference_sites').append($output);
CKEDITOR.replace('ckeditor-rs');
var editor = CKEDITOR.instances['ckeditor-rs'];
editor.setData("{!!html_entity_decode($proposal->reference_sites)!!}");
}else{
console.log("couldn't append ckeditor in rs");
}
如您所见,我正在尝试解码 HTML 并将其设置为 CKeditor 的 HTML。
$proposal->reference_sites
包含此 HTML:
<ul>
<li>site one</li>
<li>site two</li>
<li>site 3</li>
</ul>
错误:Uncaught SyntaxError: Invalid or unexpected token <
我不完全确定是什么导致了这个错误,因为当我只解码一个包含 <p>some text</p>
的变量时它插入 some text
进入编辑器。
非常感谢任何帮助!
最佳答案
试试下面的,
if($('#reference_sites').length){
$output = "<textarea id='ckeditor-rs' name='ckeditor-rs' rows='10' cols='80'></textarea>";
$('#reference_sites').append($output);
CKEDITOR.replace('ckeditor-rs');
var editor = CKEDITOR.instances['ckeditor-rs'];
editor.setData("{{$proposal->reference_sites}}");
}else{
console.log("couldn't append ckeditor in rs");
}
关于javascript - 在 ckeditor 中插入 html 会导致语法错误(laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473072/