我正在尝试替换html标签'<' '>'与'& lt;'和'& gt;'在我的文字来自TinyMCE之前做了页面回发。
使用TinyMCE v.3.x我可以这样做:
function saveCallback(element_id, html, body) {
html = html.replace(/</gi, "<");
html = html.replace(/>/gi, ">");
return html;
}
function loadCallback(type, value) {
if (type == "insert_to_editor") {
value = value.replace(/</gi, "<");
value = value.replace(/>/gi, ">");
}
return value;
}
tinyMCE.init({
...
save_callback: "saveCallback",
cleanup_callback: "loadCallback"
});
使用新的TinyMCE v.4.x我试试这种方式:
$(function () {
tinymce.init({
language: "it",
width: 500,
height: 400,
formats: false,
menubar: false,
mode: "exact",
elements: "Testo",
setup: function (editor) {
editor.on('SaveContent', function (e) {
html = editor.getContent();
html = html.replace(/</gi, "<");
html = html.replace(/>/gi, ">");
editor.getElement().value = html;
});
}
});
});
以这种方式:
$(function () {
tinymce.init({
language: "it",
width: 500,
height: 400,
formats: false,
menubar: false,
mode: "exact",
elements: "Testo",
setup: function (editor) {
editor.on('submit', function (e) {
html = editor.getContent();
html = html.replace(/</gi, "<");
html = html.replace(/>/gi, ">");
editor.getElement().value = html;
});
}
});
});
但是回发值总是包含html标记,页面返回消息“检测到潜在危险的Request.Form值”
答案 0 :(得分:2)
在 init
中尝试以下选项encoding : 'xml',
setup: function (editor) {editor.on('SaveContent', function (e) {e.content = e.content.replace(/'/g, '&apos');});}
仅供参考:改编自上面的代码并参考:
对我有用:),希望它有所帮助。
答案 1 :(得分:0)
我已经用这种方式解决了:
tinymce.init({
selector: "#MySelector",
width: ...,
height: ...,
encoding: 'xml',
setup: function (editor) { editor.on('SaveContent', function (e) { e.content = e.content.replace(/'/g, '&apos').replace(/&/g, '&'); }); }
});
答案 2 :(得分:0)
对于tinyMCE 4,请使用PostProcess事件尝试以下干净的解决方案:
tinymce.init({
language: "it",
width: 500,
height: 400,
formats: false,
menubar: false,
mode: "exact",
elements: "Testo",
init_instance_callback: function (editor) {
editor.on('PostProcess', function (e) {
e.content = e.content.replace(/</g, '<');
e.content = e.content.replace(/>/g, '>');
});
},
});