jquery - JavaScript getSelection issue -


if (!window.x) {    x = {};  }    x.selector = {};    x.selector.getselected = function() {    if (window.getselection) {      t = window.getselection();    } else if (document.getselection) {      t = document.getselection();    } else if (document.selection) {      t = document.selection.createrange().text;    }    return t;  }    function htmlspanhlt() {    element = document.createelement("span");    element.setattribute('class', 'hlt');    return element;  }    function highlight_text(sel) {    span = htmlspanhlt();    if (sel.getrangeat) {      range = sel.getrangeat(0);    }    span.appendchild(range.extractcontents());    range.insertnode(span);  }
<div class="highlight">    <p>      lorem ipsum dolor sit amet, eu ius autem labitur complectitur, eos aliquip nostrud ei, ludus erroribus vel ex. sea ex erat quaeque intellegam, et diam iusto deterruisset sea. summo minimum periculis pro ea, diam soleat id pro. cu putent inimicus eum,      hinc comprehensam ex qui. delectus appetere interesset ad mel.      <br>      <br>ius ut quis summo utroque, in sea numquam percipit eloquentiam. vidit mucius iudicabit ius id. id mel vero dolorem. sale lorem tempor ius ex, commodo reprehendunt pro cu. mel ea accumsan mentitum liberavisse, labores fabellas per te.    </p>  </div>

when highlight text dragging mouse fine. issue is: example when double click text many times or once, of course selected text highlighted normal. can u me fix issue? lot.

things think about:

range.extractcontents may not function want use. removes content page , puts in temporary documentfragment (in memory, not in page @ all).

...though looks might intentional, in highlight_text() function. without context, can't sure.

running code snippet doesn't seem mess highlighting in browser, whether clicking , dragging, or double-clicking or otherwise. on first click, highlighted text unhighlight--maybe that's mean?


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -