Finding the exact selected text index in html document which works on safari and google chrome
how to find index of selected text that is not dependent on window size and resolution? i have implemented it on IE but that code doesn't work on google chrome and safari as they dont support createrange method of javascript. this the code for IE to find start and end position
开发者_如何转开发 range = doc.selection.createRange();
range.execCommand("BackColor", false, colour);
var sourceSelEnd = range.boundingLeft;
var sourceSelStart = parseInt(sourceSelEnd);
sourceSelStart = sourceSelStart - (range.text.length);
Please help in finding start index or position of selected text. Position() and Offset() are changing on window resize and resolution change
I believe the following should do what you are looking for though it is admittedly contrived to illustrate the functionality:
function getRangeAttributes()
{
var currentRange = window.getSelection().getRangeAt(0);
return {'startContainer':currentRange.startContainer,'startOffset':currentRange.startOffset,'endContainer':currentRange.endContainer,'endOffset':currentRange.endOffset,'textLength':currentRange.toString().length}
}
Please see the following for more information:
https://developer.mozilla.org/en/DOM/range
精彩评论