>我知道可以使用[(ngModel)]="htmlContent"
检索HTML
,但是是否可以只获取文本?谢谢。
例:
此文本为粗体。这是斜体
网页内容:<b>This text is bold</b>
.<i>This is italics</i>
文本内容:This text is bold. This is italics
提出的 github 问题
您可以使用DOMParser
类分析它,然后只使用innerText
属性。
假设您在一个名为html
的变量中有 HTML,它将是
let html = '<b>This text is bold</b>.<i>This is italics</i>';
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(html, "text/html");
var text = oDOM.body.innerText;
console.log(text);
有关解析器的更多信息,请访问 https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML
是的!只需使用以下 innerHTML 属性:
<div [innerHTML]="model.property"></div>
我想你在谈论这个
1.-HTML文件添加ngModelChange:
<div class="NgxEditor__Wrapper">
<ngx-editor-menu [editor]="editor"> </ngx-editor-menu>
<ngx-editor
[editor]="editor"
[(ngModel)]="html"
[disabled]="false"
[placeholder]="'Ingresa el texto...'"
(ngModelChange)="editorChange($event)"
></ngx-editor>
</div>
2.-组件文件。
2.1.- 将类导入到Html:
import { Editor, toHTML } from 'ngx-editor';
2.2.-创建函数来捕获事件:
editorChange(event: any){
const htmlTexEditor = toHTML(this.html);
console.log(htmlTexEditor);
}
extractContent(htmlCode: string) {
let span = document.createElement('span');
span.innerHTML = htmlCode;
return span.textContent || span.innerText;
};
这可能对某人有用