首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js or jquery 字符串 安插 字符 的 位置 计算

2013-02-27 
jsor jquery字符串 插入 字符 的 位置 计算如何获取插入字符的位置?比如:[解决办法]楼主是想找到最新一次

js or jquery 字符串 插入 字符 的 位置 计算
  如何获取插入字符的位置?比如:

                            
[解决办法]
楼主是想找到最新一次插入的内容的位置是吧?

大概写了个。。有个小bug就是如果插入的内容中间没有其他的内容分隔会找不到正确的位置。明天了再看看。。

function getNewInsertIndex(oldArray,newArray){
  if(oldArray.length==newArray.length)alert('没有找到或者插入新内容!');
  else{
    var index=0;
    for(var i=0,j=newArray.length;i<j;i++){
      index+=newArray[i].length
      if(newArray[i]!=oldArray[i]){index+=i*ins.length;/*加上分隔字符长度*/ alert('新插入的内容位置为'+index);break;}
    }
  }
}
var txt = 'This is some sample text. Select this text.'  ;
var ins = "AAA "
var txt_ins = 'This is AAA some sample text. Select this text.'  ;
var arr=[],newarr=txt_ins.split(ins);
getNewInsertIndex(arr,newarr);
arr=newarr;//交换位置,每次更新过内容后记得交换位置

txt_ins ='This AAA is AAA some sample text. Select this text.'  ;
newarr=txt_ins.split(ins)
getNewInsertIndex(arr,newarr)
arr=newarr;//

txt_ins ='AAA This AAA is AAA some sample text. Select this text.' 
newarr=txt_ins.split(ins)
getNewInsertIndex(arr,newarr)
arr=newarr;//

txt_ins ='AAA This AAA is AAA some sample text. AAA Select this text.' 
newarr=txt_ins.split(ins)
getNewInsertIndex(arr,newarr)
arr=newarr;//

txt_ins ='AAA This AAA is AAA some sample text. AAA Select this AAA text.' 
newarr=txt_ins.split(ins)
getNewInsertIndex(arr,newarr)
arr=newarr;//

txt_ins ='AAA This AAA is AAA some sample text. AAA Select this AAA text AAA .' 
newarr=txt_ins.split(ins)
getNewInsertIndex(arr,newarr)
arr=newarr;//

热点排行
Bad Request.