を使用して、単語をカバーするように範囲を作成したとしrange.expand('word')
ます。通常、次の単語を追加するには、 と書きrange.moveEnd('word', 1)
ます。しかし、これは Webkit では機能しないようです。おそらく、別の方法で実装する必要がありますか?
1319 次
1 に答える
3
あなたはTextRange
IEでのみ完全に実装されているsについて話している。他のブラウザーは代わりにDOM Level 2 Rangeオブジェクトを使用します。これは、ほとんどの点で TextRanges よりもはるかに優れていますが、expand()
. ただし、最近の WebKit ブラウザーは のバージョンを実装してexpand()
おり、Webkit と Firefox 4 の両方に、同様の機能を提供modify()
するオブジェクトのメソッドがあります。Selection
例: http://jsfiddle.net/bzU22/1/
<script type="text/javascript">
function expandSelection() {
if (window.getSelection && window.getSelection().modify) {
var sel = window.getSelection();
sel.modify("extend", "forward", "word");
} else if (document.selection && document.selection.type == "Text") {
var range = document.selection.createRange();
range.moveEnd("word", 1);
range.select();
}
document.getElementById("test").focus();
}
</script>
<input type="button" unselectable onclick="expandSelection();" value="Expand">
<p contenteditable="true" id="test">Hello, this is some test text.
Select a word and then press the 'Expand' button.</p>
于 2011-01-17T01:02:57.033 に答える