function stripTags(html) {
// 创建一个临时的DOM节点
const tempDiv = document.createElement("div");
// 将HTML字符串设置为该节点的innerHTML
tempDiv.innerHTML = html;
// 返回节点的textContent,即去除所有HTML标签后的纯文本内容
return tempDiv.textContent || tempDiv.innerText || "";
}
// 示例用法
const htmlString = "<p>这是一个<strong>测试</strong>字符串。</p>";
const strippedString = stripTags(htmlString);
console.log(strippedString); // 输出: 这是一个测试字符串。
stripTags 函数用于从 HTML 字符串中移除所有的 HTML 标签,只保留纯文本内容。tempDiv) 并将 HTML 字符串赋值给它的 innerHTML 属性来实现这一点。tempDiv 的 textContent 或 innerText,这样就得到了不含任何 HTML 标签的纯文本。上一篇:js 数组 indexof
下一篇:js map转对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站