对于单行文本框和密码输入框,可以利用maxlength属性控制用户输入的字符个数。
对于多行文本,maxlength为自定义属性,其值最多输入的字符的个数,在onkeypress事件发生时则调运返回LessThan()函数返回值,函数如下
详细代码
代码如下:
function LessThan(oTextArea){
//返回文本框字符个数是否符号要求的boolean值
return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
script>
2.设置鼠标经过自动选择文本
首先是鼠标经过时自动聚焦 onmouseover = "this.focus"
其次是 onfocus = "this.select()"
代码实例:
代码如下:
对于多个代码实例,可以使用以下代码进行聚焦
代码如下:
function myFocus() {
this.focus();
}
function mySelect() {
this.select();
}
window.onload = function() {
var oForm = document.forms["myForm1"];
oForm.name.onmouseover = myFocus;
oForm.name.onfocus = mySelect;
}
script>