项目需求,用户要能够输入和点击外面的公式去插入到textaera中,试了好几种方法,有的是在谷歌下好使,在ie下不好使,最后找到了下面这个方法,目前在ie8以上都可以生效。直接上代码
1 function insertatcursor(myfield, myvalue) { 2 //ie support 3 if (document.selection) { 4 myfield.focus(); 5 sel = document.selection.createrange(); 6 sel.text = myvalue; 7 } 8 //mozilla and others 9 else if (myfield.selectionstart || myfield.selectionstart == '0') { 10 var startpos = myfield.selectionstart; 11 var endpos = myfield.selectionend; 12 myfield.value = myfield.value.substring(0, startpos) 13 + myvalue 14 + myfield.value.substring(endpos, myfield.value.length); 15 myfield.selectionstart = startpos + myvalue.length; 16 myfield.selectionend = startpos + myvalue.length; 17 } else { 18 myfield.value += myvalue; 19 } 20 } 21
调用也相当的简单
insertatcursor(‘dom节点’,‘内容’);
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论