range.startOffset
Internet Explorer 8 以前と同等の機能を得るにはどうすればよいですか?
つまり、コンテナ内で範囲が始まる文字数を教えてくれる、範囲に対して呼び出すことができる関数が欲しいのです。
range.startOffset
Internet Explorer 8 以前と同等の機能を得るにはどうすればよいですか?
つまり、コンテナ内で範囲が始まる文字数を教えてくれる、範囲に対して呼び出すことができる関数が欲しいのです。
IEでDOMRangeを実装する場合は、私のRangyライブラリ(http://code.google.com/p/rangy/ )を使用できます。
var range = rangy.getSelection().getRangeAt(0);
alert(range.startOffset);
ここにコード例があります。内部のコメントを参照してください。
<html>
<head>
<title>Test</title>
<script type="text/javascript">
<!--
function fx()
{
//create a range of selection
var rng = document.selection.createRange();
//if nothing is selected return null
if(rng.text=='')return null;
//create a second range of selection
var rng2 = document.selection.createRange();
//let the 2nd range encompass the whole element
rng2.moveToElementText(rng.parentElement())
//move the end-point of the 2nd range to the start-point of the 1st range
rng2.setEndPoint('EndToStart', rng);
//return the length of the text in the 2nd range
return(rng2.text.length);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="alert(fx())" value="select some text below and then click me">
<p>1234<b style="color:red">5678</i>90</p>
</body>
</html>