【问题标题】:How to strip html tags out of selection如何从选择中去除html标签
【发布时间】:2022-02-09 00:08:06
【问题描述】:

我想从内容可编辑 div 内的选定文本中去除 html 标记。假设选定的文本是这样的:

<div contenteditable="true">
  <span style="color:red">some text</span>
</div>

所以我尝试了这个:

window.getSelection().anchorNode.parentNode.replace(/<\/?[^>]+>/gi, '');

但我得到一个错误,说替换不是一个函数。我也在下面尝试过:

window.getSelection().anchorNode.parentNode.remove();

但这也会删除文本。这里有什么交易?

谢谢。

【问题讨论】:

标签: javascript highlight


【解决方案1】:

像这样

const editable = document.querySelector('div');
const button = document.querySelector('button');

button.addEventListener('click', () => {
  alert(editable.innerText);
});
   <div contenteditable="true">
    <span style="color:red">some text</span>
   </div>
   
   <button>Show me the text</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2012-08-07
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多