Javascript SummerNote 插入HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34170950/
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
SummerNote insertHtml
提问by Justin
I have a 'template' selector, that enables people to change the 'text/html' of the current textarea.
我有一个“模板”选择器,它使人们能够更改当前文本区域的“文本/html”。
So when they change templates, I need to update the Summernote HTML. I see the method for (insertText), but it doesn't work for HTML.
因此,当他们更改模板时,我需要更新 Summernote HTML。我看到了 (insertText) 的方法,但它不适用于 HTML。
Is there a solution for HTML version?
HTML 版本有解决方案吗?
I thought there was a .code() solution, but doesn't seem to be working? I receive an error, "Not a function"
我以为有一个 .code() 解决方案,但似乎不起作用?我收到一个错误,“不是一个函数”
Any help would be greatly appreciated!
任何帮助将不胜感激!
$('.dialogMessageArea').summernote({
height:'230px',
focus:true,
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
['fontsize', ['fontsize']],
['para', ['ul', 'ol', 'paragraph']],
['table', ['table']],
['misc', ['fullscreen','undo','redo']]
]
});
$(document).on('change', '.messageTemplate', function() {
var templateId = $(this).selected().attr('value');
if(templateId) {
$.ajax({
type: "POST",
dataType: "json",
url: '/cont/templates/GetTemplate',
data: {'templateId': templateId},
success: function (data) {
$('.subjectMessage').val(data.results[0].subject);
if(data.results[0].template) {
$('.dialogMessageArea').code(data.results[0].template);
}
},
error: function () {
alert('We were not able to get your template');
}
});
}
});
console.log($('.dialogMessageArea'));
console.log("<b>Welcome</b><h3>hello world</h3>");
回答by Pooya Mobasher Behrooz
According to the api (https://summernote.org/getting-started/#get--set-code),
根据 api ( https://summernote.org/getting-started/#get--set-code),
to change wysiwyg's value you should use summernote function with 'code' string as first parameter and your content as second one
要更改所见即所得的值,您应该使用带有“代码”字符串作为第一个参数和您的内容作为第二个参数的 summernote 函数
Like this :
像这样 :
$('.dialogMessageArea').summernote('code', data.results[0].template);
回答by Step
.summernote('pasteHTML', '<b>inserted html</b> ');
回答by Abhinav
This should actually work
这实际上应该有效
$('.dialogMessageArea').code('some value you want');
NOTE:
笔记:
HTML inside code()
function has to be a string. I don't think it will work if you are trying to insert an invalid HTML
contents.
code()
函数内部的 HTML必须是一个字符串。如果您尝试插入invalid HTML
内容,我认为它不会起作用。
Try doing this:
尝试这样做:
Create a hidden div and then in your AJAX
success
method try to insert the datas from the AJAX and then retrieve it using jquery's text()
function.
创建一个隐藏的 div,然后在您的AJAX
success
方法中尝试从 AJAX 插入数据,然后使用 jquery 的text()
函数检索它。
<div style="display:none;" id="hidden_div">
success: function (data) {
$('.subjectMessage').val(data.results[0].subject);
if(data.results[0].template) {
$('#hidden_div').html(data.results[0].template);
$('.dialogMessageArea').code($('#hidden_div').text());
}
},
回答by Marcel Kohls
If you don't want to replace all the content from the note, you can use the insertHTML command from the execCommand WebAPI.
如果不想替换笔记中的所有内容,可以使用 execCommand WebAPI 中的 insertHTML 命令。
As summernote is based on the execCommand WebAPI, we can use directly its resources.
So the most simple way to add a HTML content to the summernote can be using the insertHtml
parameter like this:
由于 Summernote 基于execCommand WebAPI,我们可以直接使用其资源。因此,将 HTML 内容添加到 Summernote 的最简单方法可以使用如下insertHtml
参数:
document.execCommand('insertHtml', null, '<p>My text <span style="color:red;">here</span></p>');
回答by Naveen R
This works best for me
这对我最有效
var html = '<h2>Hello World!!</h2>';
$('#divID').summernote('code', html);
var html = '<h2>Hello World!!</h2>';
$('#divID').summernote('code', html);