如何在javascript中向后扩展选择?

如何在javascript中向后扩展选择?,javascript,range,selection,Javascript,Range,Selection,我使用的是一个用户界面,它提供了选择文本的按钮,而不是鼠标点击和拖动。用户获得一个文本框选择以输入要选择的文本,以及一组按钮: 选择 将选择扩展到下一个单词 将选择扩展到上一个单词 该代码非常适合上面的前两个任务。向后扩展选择是可行的,但不是我想要的方式 所需功能: 这是一个例子。在下面的输入框中输入本文中的一些文本,然后按选择按钮进行选择。 选择:来自此 向后扩展选择:(此时)从 向后扩展选择:(应选择)此文件中的文本 i、 e.我想向后移动锚一个单词。我怎样才能做到这一点 getSelec

我使用的是一个用户界面,它提供了选择文本的按钮,而不是鼠标点击和拖动。用户获得一个文本框
选择
以输入要选择的文本,以及一组按钮:

  • 选择
  • 将选择扩展到下一个单词
  • 将选择扩展到上一个单词
  • 该代码非常适合上面的前两个任务。向后扩展选择是可行的,但不是我想要的方式

    所需功能:
    这是一个例子。在下面的输入框中输入本文中的一些文本,然后按选择按钮进行选择。
    选择:来自此
    向后扩展选择:(此时)从
    向后扩展选择:(应选择)此文件中的文本
    i、 e.我想向后移动
    一个单词。我怎样才能做到这一点

    getSelectionText=function(){
    句子=document.getElementById(“句子”);
    target=document.getElementById(“目标”);
    selection=window.getSelection();
    range=document.createRange();
    index=句子.innerHTML.indexOf(target.value);
    range.setStart(句子.firstChild,索引);
    range.setEnd(句子.firstChild,(index+target.value.length));
    selection.removeAllRanges();
    选择。添加范围(范围);
    }
    removeSelection=函数(){
    selection=window.getSelection();
    selection.removeAllRanges();
    }
    extendSelection=函数(buttonId){
    var元素=document.getElementById(buttonId);
    如果(element.id==“上一个”){
    var selection=window.getSelection();
    选择。修改(“扩展”、“左”、“字”);
    }else if(element.id==“下一步”){
    var selection=window.getSelection();
    选择。修改(“扩展”、“右”、“字”);
    }
    }
    
    这是一个例子。在下面的输入框中输入本文中的一些文本,然后按选择按钮进行选择。
    
    选择:

    删除所选内容 扩展选择 <先前
    扩展选择>下一步
    不是很漂亮,但是下面的解决方案对我很有效。下面的代码段包含一个附加方法
    previousWord
    ,该方法将
    语句
    转换为数组,并返回紧靠所选内容之前的单词。按
    Move Selection
    可重置
    Selection
    的开始以包括上一个单词,反复按也可以正常工作

    getSelectionText=function(){
    句子=document.getElementById(“句子”);
    target=document.getElementById(“目标”);
    selection=window.getSelection();
    range=document.createRange();
    index=句子.innerHTML.indexOf(target.value);
    range.setStart(句子.firstChild,索引);
    range.setEnd(句子.firstChild,(index+target.value.length));
    selection.removeAllRanges();
    选择。添加范围(范围);
    }
    removeSelection=函数(){
    selection=window.getSelection();
    selection.removeAllRanges();
    }
    extendSelection=函数(buttonId){
    var元素=document.getElementById(buttonId);
    如果(element.id==“上一个”){
    var selection=window.getSelection();
    选择。修改(“扩展”、“左”、“字”);
    }else if(element.id==“下一步”){
    var selection=window.getSelection();
    选择。修改(“扩展”、“右”、“字”);
    }else if(element.id==“Move”){
    var selection=window.getSelection();
    range=document.createRange();
    target=“”+上一个单词()+”;
    indexPW=句子.innerHTML.indexOf(目标);
    range.setStart(句子.firstChild,indexPW+1);
    range.setEnd(句子.firstChild,(index+selection.toString().length));
    selection.removeAllRanges();
    选择。添加范围(范围);
    }
    }
    previousWord=函数(){
    var pw=“”;
    var-station=document.getElementById(“句子”).innerHTML;
    var selection=window.getSelection();
    索引=句子。索引(选择);
    var sentary=句子。拆分(“”);
    var firstWord=selection.toString().split(“”[0];
    var firstWordIndex=sentArray.indexOf(firstWord);
    如果(firstWordIndex>=1){
    pw=sentArray[firstWordIndex-1];
    }否则{
    pw=sentary[0];
    }
    返回pw;
    }
    这是一个例子。选择任意长度的文本块,然后按下面的按钮。
    
    选择:

    删除所选内容 撤消扩展 扩展选择>下一步 移动选择