実際には、は、HTMLエディタ(contenteditable iframeを使用します)内の特定のノードにカーソルを設定しようとしています。たとえば、いくつかの段落があり、カーソルを前の段落の先頭に移動したいとします。 不幸にも、Internet Explorerの範囲オブジェクトはsetStartBefore
とsetStartAfter
をサポートしていません。 The ierange projectはオプションではありません。私が探している解決策は、そのままの状態でIEで動作する必要があります。IE(7-8)を使用してHTMLエディタでカーソルを設定するには
IEでカーソルを設定するにはどうすればよいですか? Firefoxので
解決策は単純です:
// sets the cursor position (start defines, if cursor is needed at the start or end of the node)
function setCursor(editor, node, start){
var tn = editor.getDoc().createTextNode("."); // gets the editors document
if (start){
node.insertBefore(tn, node.firstChild);
}
else node.appendChild(tn);
rng = editor.selection.getRng(); // gets the browsers range object for the users selection
rng.selectNode(tn);
rng.setStartBefore(tn);
rng.setStartAfter(tn);
ed.selection.setRng(rng);
node.removeChild(tn); // removes the caret node - curser is placed now
}
ありがとう、作品(テストFF、IE8、Google Chrome)。 IE8がstartがfalseのときに次の行の先頭にカーソルを置く理由を知っていますか(FFは現在の段落の終わりまで設定します)? – Thariama
これは、IEのTextRangeと選択処理のものが独特で迷惑なものだからです。実際にIEでは、ダミー要素が必要な場合があります。私は試して修正します。 –
改訂........ –