javascript获得INPUT中的光标位置

使用JavaScript获取input光标,这里只介绍常用的IE和Firefox中的方法:

在Firefox中非常简单,在IE中非常强大,两种不同的获取方式如下:

function GetPosition(input)
{
	if($.browser.msie)
	{
		var cuRange=document.selection.createRange();
		var tbRange=input.createTextRange();
		tbRange.collapse(true);
		tbRange.select();
		var headRange=document.selection.createRange();
		headRange.setEndPoint("EndToEnd",cuRange);
		var pos=headRange.text.length;
		cuRange.select();
		return pos;
	}
	else
		return input.selectionStart;
}

Leave a Reply

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">