如何在TextBox或TextArea中获取位置光标 [英] How To Get Position Cursor in TextBox Or TextArea

查看:114
本文介绍了如何在TextBox或TextArea中获取位置光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

<HTML>
<HEAD><TITLE>Selection</TITLE>
<SCRIPT type="text/javascript">
function disp()
{
  var text = document.getElementById("text");
  //var t = text.value.substr(text.selectionStart,text.selectionEnd-text.selectionStart);
  //alert(t+"\nSelectionStart:"+text.selectionStart);
  

}
</SCRIPT>
</HEAD>
<BODY>

<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR>
<INPUT type="button"  value="Select text and click here"   önclick="disp();"/>
</BODY>
</HTML>







SelectionStart不适用于IE但在FireFox工作。

请帮帮我




SelectionStart is not Work In IE but In FireFox Work.
Please Help me

推荐答案

来源: http://stackoverflow.com/questions/1891444/how-can-i -get-cursor-position-in-a textarea [ ^ ]



用于跨浏览器兼容性,您需要测试一些功能:



1. selectionStart(你已经发现)

2.选择(显示在上面的链接中)



基本代码:

Source: http://stackoverflow.com/questions/1891444/how-can-i-get-cursor-position-in-a-textarea[^]

for cross-browser compatibility you will need to test for a few capabilities:

1. selectionStart (which you have already found out)
2. selection (which is shown in the link above)

essential code:
var pos = 0;
if("selectionStart" in el) {
   pos = el.selectionStart;
} else if("selection" in document) {
   el.focus();
   var Sel = document.selection.createRange();
   var SelLength = document.selection.createRange().text.length;
   Sel.moveStart("character", -el.value.length);
   pos = Sel.text.length - SelLength;
}
return pos;


set autofocus =autofocus在文本框中。这将在HTML 5中工作。
set autofocus="autofocus" in the textbox. This will work in HTML 5.


let cursorIndex =


这篇关于如何在TextBox或TextArea中获取位置光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆