简体   繁体   English

使用 JavaScript 从所选文本中删除 Html 标签

[英]Remove Html tag from selected text with JavaScript

I am new at JavaScript and I tried to add a new tag to the selected word.我是JavaScript新手,我尝试向所选单词添加新标签。
I did it like this:我是这样做的:

function addBoldTag() {
                 var highlight = window.getSelection();
               if(highlight.toString().length>=1){
               var range = highlight.getRangeAt(0); 
                var selectionContents = range.extractContents(); 
                 var boldtag = document.createElement("b"); 
                 boldtag.style.cssText = " color :blue; ";
                 boldtag.appendChild(selectionContents); 
                 range.insertNode(boldtag); 
                 highlight.removeAllRanges(); 
                 }
}

I have a new issue now, I'd like to unwrap or remove selected word's tag, the way HTMLeditors does it, but I can't find a way to do it.我现在有一个新问题,我想按照HTMLeditors的方式解开或删除选定单词的标签,但我找不到方法。 I've found Jquery , but I have to do with pure javascript .我找到了Jquery ,但我必须使用pure javascript

Please have a look below:请看下面:

 const selectedRange = window.getSelection().getRangeAt(0); const element = document.createElement('strong'); selectedRange.surroundContents(element);

I hope it would help you out我希望它能帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM