selectNode() not working in IE8
I need to select a node in tiny mce, I'm trying following code. It works in non-IE browsers, but on IE it gives error as - Error: Object doesn't support this property or method
Code -
rng = ed.selection.getRng();
rng.selectNode(tn);
The second l开发者_开发百科ine causes the error. Is there any other IE compatible method for this ?
The reason for this is the different range object you get. In FF you get an object which offers you several functions like selectNode()
. The IE range only presents you a textrange object which contains only properties.
The solution to this is easy:
// true is important here - causes getRng to return a DOM Range and not a text range (IE)
rng = ed.selection.getRng(true);
rng.selectNode(tn);
I read about TextRange object and was able to do what I intended. I wanted to set my cursor inside a span. I could do it using TextRange object properties and methods as follows -
range.moveToElementText (node); //node is the span in which I wanted to place my cursor
range.select ();
moveToElementText - Aligns the start and end points of the current TextRange object to the text content of the specified element. I added an nbsp; to the span and with the above code could replace it, by my caret.
精彩评论