如何在 angular 中对 ckeditor 中的 HTML 输入进行编码
How to encode the HTML input in ckeditor in angular
我在我的 angular 项目中使用 ng2-ckeditor 我需要将 < 和 > 编码为 < ,> ,那么如何编码输入 HTML
.html
<ckeditor> </ckeditor>
<button type="button" (click) ="submit()">Click Me!</button>
.ts
import { Component, OnInit } from '@angular/core';
declare var CKEDITOR: any;
@Component({
selector: 'app-html-editor',
templateUrl: './html-editor.component.html',
styleUrls: ['./html-editor.component.css']
})
export class HtmlEditorComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
submit() {
var editor = CKEDITOR.instances.editor1;
var data = editor.getData();
console.log(data)
}
}
你可以转换任何你想要的字符。下面是将 str 作为输入并 return 转换后的字符串的代码。
function convertHTML(str) {
// :)
var Regcheck = /\W\s/gi;
var htmlListObj = {'&':"&",
"<":"<",
">":">",
'"':""",
"'":"'"};
var a = str.split("");
var b =[];
for (var i in a) {
if (htmlListObj.hasOwnProperty(a[i])) {
b.push( htmlListObj[a[i]]);
}else{
b.push(a[i]);
}
}
var c = b.join("");
return c;
}
我在我的 angular 项目中使用 ng2-ckeditor 我需要将 < 和 > 编码为 < ,> ,那么如何编码输入 HTML
.html
<ckeditor> </ckeditor>
<button type="button" (click) ="submit()">Click Me!</button>
.ts
import { Component, OnInit } from '@angular/core';
declare var CKEDITOR: any;
@Component({
selector: 'app-html-editor',
templateUrl: './html-editor.component.html',
styleUrls: ['./html-editor.component.css']
})
export class HtmlEditorComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
submit() {
var editor = CKEDITOR.instances.editor1;
var data = editor.getData();
console.log(data)
}
}
你可以转换任何你想要的字符。下面是将 str 作为输入并 return 转换后的字符串的代码。
function convertHTML(str) {
// :)
var Regcheck = /\W\s/gi;
var htmlListObj = {'&':"&",
"<":"<",
">":">",
'"':""",
"'":"'"};
var a = str.split("");
var b =[];
for (var i in a) {
if (htmlListObj.hasOwnProperty(a[i])) {
b.push( htmlListObj[a[i]]);
}else{
b.push(a[i]);
}
}
var c = b.join("");
return c;
}