Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Internet Explorer等效范围.startOffset_Javascript_Internet Explorer_Textrange - Fatal编程技术网

Javascript Internet Explorer等效范围.startOffset

Javascript Internet Explorer等效范围.startOffset,javascript,internet-explorer,textrange,Javascript,Internet Explorer,Textrange,如何获得与Internet Explorer 8及以下版本中的功能等效的功能 也就是说,我想要一个可以在某个范围内调用的函数,该函数将告诉我该范围开始时其容器中有多少个字符。这里是一个示例代码,请参见其中的注释: <html> <head> <title>Test</title> <script type="text/javascript"> <!-- function fx() { //create a range

如何获得与Internet Explorer 8及以下版本中的功能等效的功能


也就是说,我想要一个可以在某个范围内调用的函数,该函数将告诉我该范围开始时其容器中有多少个字符。

这里是一个示例代码,请参见其中的注释:

<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>

下面是一个示例代码,请参见下面的注释:

<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>

如果希望在IE中实现DOM范围,可以使用my Rangy库:


如果希望在IE中实现DOM范围,可以使用my Rangy库:


这并不完全等同于范围的startOffset,它只相对于其直接容器节点。但是,这很可能是OP想要的,在其他浏览器中很难实现。因此,这是针对这个问题的特定于IE的答案吗:?这是针对您的特定于IE的问题的特定于IE的答案。这并不完全等同于范围的startOffset,它仅相对于其直接容器节点。但是,这很可能是OP想要的,这在其他浏览器中很难实现。因此,这是针对这个问题的特定于IE的答案吗:?这是针对您的特定于IE的问题的特定于IE的答案。range.startOffset是仅相对于其直接容器的偏移量,并且仅当容器是文本节点时才是字符偏移量。这就是你想要的吗?@Tim:你说得对;这不是我想要的。我创建了一个新问题:range.startOffset是仅相对于其直接容器的偏移量,并且仅当容器是文本节点时才是字符偏移量。这就是你想要的吗?@Tim:你说得对;这不是我想要的。我提出了一个新问题:你的图书馆规则。谢谢你,蒂姆,你的图书馆规则。谢谢你,蒂姆。