<! DOCTYPE html>"en">"UTF-8"> <title>title</title>class="app"contenteditable="true"> <span id="content">12345675345</span> <p id="Content2">12345675345</p></div><button id="btn"> Show selected text </button></body><script>selction=window.getselection (); ConstBTN = Document.queryselector ('#btn'); ConstApp = Document.queryselector ('. App'); ConstContent = Document.queryselector ('#content'); ConstContent2 = Document.queryselector ('#content2'); App.focus (); Btn.onclick=function () {let selction, text; Console.log (Selction.tostring ());//Output selected textalert (selction); } Let range= Selction.getrangeat (0); Range.setstart (Selction.anchornode,selction.anchoroffset); Range.setend (Selction.focusnode,selction.focusoffset+6); Selction.removeallranges (); Selction.addrange (range);//actively add selected textConsole.log (selction);</script>View CodeHTML Rich Text Editor related--output selected text and active selection text