前端使用JS从HTML中提取文本

function extractContent(html) {
    return new DOMParser()
        .parseFromString(html, "text/html")
        .documentElement.textContent;
}

使用示例

let result = extractContent('<p>Hello World</p>')

console.log(result) // 'Hello World'

 

posted @ 2023-02-16 11:49  脆皮鸡  阅读(359)  评论(0)    收藏  举报