如何在jQuery/Javascript中获取文本框中选定的单词索引

如何在jQuery/Javascript中获取文本框中选定的单词索引,javascript,jquery,html,twitter-bootstrap-3,Javascript,Jquery,Html,Twitter Bootstrap 3,我正在尝试使用jQuery从文本框中单击鼠标获取所选单词的索引 我应该在点击时获得单词索引,如下所示: WIFI -> 0 , -> 1 Parking -> 2 Available -> 3 ! -> 4 ! -> 5 Internet -> 6 Includes -> 7 ? -> 8 ? -> 9 . -> 10 IS -> 11 Internet -> 12 Working -> 13 所以

我正在尝试使用jQuery从文本框中单击鼠标获取所选单词的索引

我应该在点击时获得单词索引,如下所示:

WIFI -> 0
, -> 1
Parking -> 2
Available -> 3
! -> 4 
! -> 5
Internet -> 6
Includes -> 7
? -> 8 
? -> 9 
. -> 10 
IS -> 11 
Internet -> 12 
Working -> 13
所以在选择互联网时,它应该返回12作为索引位置

$("#idTable1 > tbody > tr").on('click', function (e) 
{
var start = document.getElementById("inputTxt").selectionStart;
var end = document.getElementById("inputTxt").selectionEnd;
目前,我正在使用下面的方法,但使用这种方法,我只得到所选单词的字符位置

$("#idTable1 > tbody > tr").on('click', function (e) 
{
var start = document.getElementById("inputTxt").selectionStart;
var end = document.getElementById("inputTxt").selectionEnd;
但是在点击这个词的时候,我应该得到这个句子中标记词的索引


任何指针都有助于解决此问题。

您可以尝试将文本框中的字符串按“”拆分,然后使用
var start=document.getElementById(“inputxt”).selectionStart计算数组中包含要查找的单词的元素

var arr = Split(document.getElementById("inputTxt").value, " ");
var start = document.getElementById("inputTxt").selectionStart;
var arrpos = 0;
var idx = 0;
for(idx=0; arrpos<start & idx < arr.lenght; idx++){
    arrpos += arr[idx].lenght;
}
return idx;
var arr=Split(document.getElementById(“inputxt”).value,”;
var start=document.getElementById(“inputxt”).selectionStart;
var-arrpos=0;
var-idx=0;

for(idx=0;arrposWelcome to SO。您的问题可能不符合指导原则,因此正在进行向下投票。请检查并修复您的问题。如果示例输入使用了正确的语法,这将非常容易。您可以删除除a-z之外的所有字符,然后使用空格作为分隔符拆分为一个数组。