Calculate Position of selected text javascript/JQuery?

前端 未结 2 836
北海茫月
北海茫月 2020-11-30 07:30

How to retrieve the position of selected text by calculating it\'s offset immediate after body tag?

For Example consider the following html,



        
2条回答
  •  醉酒成梦
    2020-11-30 07:33

    Use following java script function for Highlighting the html page..

    function stylizeHighlightedString() 
    {
        var range               = window.getSelection().getRangeAt(0);
        var selectionContents   = range.extractContents();
        var span                = document.createElement("span");
        span.appendChild(selectionContents);
        span.setAttribute("class","uiWebviewHighlight");
    
        span.style.backgroundColor  = "red";
        span.style.color            = "white";
    
        range.insertNode(span);
    }
    

提交回复
热议问题