<html>
<head>
<title>Test Page</title>
</head>
<body>
<form>
Input:
<textarea rows="5" cols="50" name="aaa"></textarea>
</form>
</body>
</html>
<script type="text/javascript">
function checkFocus(obj)
{
if(obj.value.length > 0)
{
var ss = obj.value;
ss = getTrimStr(ss);
if(ss.length < 1)
{
setCaretTo(obj, 0);
return;
}
else
{
var pos = ss.length;
var getPos = getPosTrim(obj);
if(getPos >= pos)
{
setCaretTo(obj, pos);
}
}
}
}
function getTrimStr(value)
{
return value.replace(/(^/s*)|(/s*$)/g,"");
}
function setCaretTo(obj, pos)
{
if(obj.createTextRange)
{
var range = obj.createTextRange();
range.move("character", pos-1);
range.select();
}
else if(obj.selectionStart)
{
obj.focus();
obj.setSelectionRange(pos, pos);
}
}
function getPosTrim(obj)
{
if(obj.selectionStart)
{
var end = obj.selectionStart;
return end;
}
else
{
obj.focus();
var workRange=document.selection.createRange();
obj.select();
var allRange=document.selection.createRange();
workRange.setEndPoint("StartToStart",allRange);
var len=workRange.text.length;
workRange.select();
return len;
}
}
</script>
textarea的相关操作
最新推荐文章于 2024-08-18 23:01:41 发布