JS expand empty range in IE
I have a caret in the textarea (no selection). I need to make selection out of it.
For example: "This is a te|xt"var caret = document.selection.createRange (); // got empty range between "e" and "x"
caret.moveEnd('character'); // Move e开发者_如何转开发ndpoint one character right, now it must be "x"
alert (caret.text); // empty !!!
What is wrong?
You don't call "moveEnd" on the selection object - you call it on the TextRange ("caret"):
var caret = document.selection.createRange (); // got empty range between "e" and "x"
caret.moveEnd('character'); // Move endpoint one character right, now it must be "x"
caret.select(); // make the selection equal the range
alert (caret.htmlText);
edit I'm also editing this to make it so that the "alert" actually works :-)
Here is an example page: http://gutfullofbeer.net/range.html
I think this stuff might only work in IE by the way. (Confirmed that at least Chrome doesn't do anything with that code.)
精彩评论