简体   繁体   English

JavaScript / jQuery getSelection突出显示合并span html元素

[英]JavaScript / jQuery getSelection Highlight merging the span html element

 function highlight_text(sel) { span = htmlSpanHlt(); if (sel.getRangeAt) { range = sel.getRangeAt(0); } span.appendChild(range.extractContents()); range.insertNode(span); } function htmlSpanHlt() { element = document.createElement("span"); element.setAttribute('class', 'hlt'); return element; } 
 .hlt { background: yellow; } 
 <div class="highlight"> <p> <span class="hlt">Lorem</span> ipsum dolor sit amet, zril accusata postulant at ius. Dicat etiam luptatum ea pri. Duo docendi vulputate dissentiet ut, tollit bonorum duo eu. Maiorum adolescens scriptorem usu eu, sit idque facer causae ei. Choro nostrum at pro, sumo essent suscipiantur pri ex. Quo case veritus definiebas eu, sit postulant dissentias ei.<br> <br> Mel cu porro decore, cum et simul recteque inciderint. His ne omnes sententiae, ut omittantur dissentiet accommodare sed. Pro minim necessitatibus ne, no malis integre quo. Sed utroque molestiae interpretaris ne. Oportere indoctum at has, te sale semper eum, ea nam vitae dissentiet. </p> </div> 

The word "Lorem" has a classname "hlt" which was already been highlighted. 单词“Lorem”的类名为“hlt”,已经突出显示。 What I want is: if I want to highlight another text including the word "Lorem" w/c is already been highlighted. 我想要的是:如果我想要突出另一个文本,包括“Lorem”这个词已经突出显示了w / c。 I want to merge their span html element. 我想合并他们的span html元素。 Example: If I highlight "ipsum" next to the word "Lorem" and I include also "Lorem" the result will be Lorem Ipsum. 示例:如果我突出显示“Lorem”一词旁边的“ipsum”,并且我还包含“Lorem”,结果将是Lorem Ipsum。 Can someone help me? 有人能帮我吗? Thank u. 感谢你。

How about this? 这个怎么样?

 function highlight_text(sel) { var hlt_element = document.getElementsByClassName('hlt')[0]; if (!hlt_element || sel.containsNode(hlt_element,true)) { add_hlt_element(sel); } } function add_hlt_element(sel) { var element = document.createElement("span"); var range = sel.getRangeAt(0); element.setAttribute('class', 'hlt'); element.innerHTML = sel.toString().replace(/(?:\\r\\n|\\r|\\n)/g, '<br />\\n'); range.deleteContents(); range.insertNode(element); var highlight_div = document.getElementsByClassName("highlight")[0]; highlight_div.innerHTML = highlight_div.innerHTML.replace('</span><span class="hlt">',''); } window.onmouseup = function() { var sel = window.getSelection(); highlight_text(sel); } 
 .hlt { background: yellow; } 
 <div class="highlight"> <p> Lorem ipsum dolor sit amet, zril accusata postulant at ius. Dicat etiam luptatum ea pri. Duo docendi vulputate dissentiet ut, tollit bonorum duo eu. Maiorum adolescens scriptorem usu eu, sit idque facer causae ei. Choro nostrum at pro, sumo essent suscipiantur pri ex. Quo case veritus definiebas eu, sit postulant dissentias ei.<br> <br> Mel cu porro decore, cum et simul recteque inciderint. His ne omnes sententiae, ut omittantur dissentiet accommodare sed. Pro minim necessitatibus ne, no malis integre quo. Sed utroque molestiae interpretaris ne. Oportere indoctum at has, te sale semper eum, ea nam vitae dissentiet. </p> </div> 

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

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