使用javascript与indeterminate实现三态checkbox

当checkbox的indeterminate设置为true的时候, input会变成灰色打勾的状态. IE一直支持此属性, 而Firefox却不支持.不过在最新的Firefox3.6中, 也开始支持这个属性了. 我们可以用简单的javascript来实现三态checkbox.

$("input:checkbox").live("click",function()
{
	if(this.getAttribute("checked") && !this.getAttribute("indeterminate"))
	{
		this.setAttribute("indeterminate", true);
		this.setAttribute("indeterminate-status", true);
		return false;
	}
	else if(!this.getAttribute("indeterminate") && this.getAttribute("indeterminate-status"))
	{
		this.setAttribute("checked", true);
		this.removeAttribute("indeterminate-status");
		return true;
	}
});

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;
}