编写ASP.net Ajax客户端控件ListBox(1)

IE6中关于文本框中的光标位置与选中区

Tiger Soldier posted @ 2008年4月01日 19:04 in javascript , 3884 阅读
在FireFox下,要获取一个文本框的光标位置,可以通过获取它的选择起点和终点解决:当没有选中文字时,selectionStart和selectionEnd均等于文本框的光标位置;有选中文字时,seletionStart和selectionEnd的含义与它们名字的字面含义相同。

但是IE6里没有提供这两个属性,只能通过它的TextRange对象来曲线解决:
var expobj = document.getElementById("aTextbox");
if (document.selection){               //IE
  expobj.focus();
  sel = document.selection.createRange();
  ……
}
通过document.selection.createRange(),可以返回当前文档选中部分的TextRange对象。为了防止操作到文档的其他选中区域,要首先把焦点转给要操作的文本框。
之后就可以通过更改sel来操作选中的文本,包括改变文字什么的。
要获得选中区在文本框的位置,要用曲线点的方法:把选中区开头移动到文本框的最前端,然后选中区文本的长度就是选中区末端在文本框中的位置了:
sel.moveStart("character",-expobj.value.length);
pos = sel.text.length;
同理可得选中开头处的位置。
使用sel.select()可以令改动后的选择范围生效。...
  • 无匹配
  • 无匹配
pavzi.com 说:
Jan 18, 2024 03:53:31 PM

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter