本框是表单中与用户打交道最多的元素之一,它包括单行文本框<input type="text">和多行文本框<textarea>,
更广义的还可以包括密码输入框<input type="password">。
控制用户输入字符个数
对于单行文本框<input type="text">和密码输入框<input type="password">而言,可以利用自身的maxlength属性控制用户输入字符的个数;
<input type="text" name="name" id="name" class="txt" value="姓名" maxlength="10"/>
而对于多行文本框<textarea>没有类似的属性,可以自定义类似的属性,并对onkeypress事件做相应的处理
<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onkeypress="return LessThan(this);"></textarea/>
以上代码中maxlength为自定义属性(<textarea>标签中没有这个maxlength属性),其值为最多允许输入的字符的个数,
在onkeypress事件发生时则调用返回LessThan()函数的返回值,函数如下:
function LessThan(oTextArea){
//返回文本框字符个数是否符号要求的boolean值
return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
实例:
<html>
<head>
<title>控制textarea的字符个数</title>
<style>
<!--
form{
padding:0px;
margin:0px;
font:14px Arial;
}
input.txt{ /* 文本框单独设置 */
border: 1px inset #00008B;
background-color: #ADD8E6;
}
input.btn{ /* 按钮单独设置 */
color: #00008B;
background-color: #ADD8E6;
border: 1px outset #00008B;
padding: 1px 2px 1px 2px;
}
-->
</style>
<script language="javascript">
function LessThan(oTextArea){
//返回文本框字符个数是否符号要求的boolean值
return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="addInfo.aspx">
<p><label for="name">请输入您的姓名:</label>
<input type="text" name="name" id="name" class="txt" value="姓名" maxlength="10"></p>
<p><label for="comments">我要留言:</label><br>
<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onkeypress="return LessThan(this);"></textarea></p>
<p><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
<input type="reset" name="btnReset" id="btnReset" value="Reset" class="btn"></p>
</form>
</body>
</html>
设置鼠标经过时自动选择文本
通常是在用户名、密码等文本框中希望鼠标指针经过时自动聚焦,并且能够选中默认值以便用户直接删除。
首先是鼠标指针经过时自动聚焦,代码如下
onmouseover="this.focus()"
其次是聚焦后自动选中所有文本,代码如下:
onfocus="this.select()"
如:<label for="name">请输入您的姓名:</label>
<input type="text" name="name" id="name" class="txt" value="姓名" onmouseover="this.focus()" onfocus="this.select()">
javascript分离实现自动选择文本
分享成果,随喜正能量】我们毕生的任务就是做一个优秀的普通人。这个优秀的普通人,热爱世界,热爱万物,热爱众生,然后踏踏实实地去寻找到一个自己内心喜欢又有时代价值的事情。一个人一辈子能够做好一两件事就很好了。。
我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的劳动效率,而且可以提高数据的准确度。我推出的VBA系列教程共十套(本文的最后附有VBA教程目录和VBA工具目录),现在已经全部完成。
如果您VBA是入门阶段,可以打包选择7.1.3.9教程,第7套教程是入门,第1套教程是入门后的提高,第3套教程字典是必备的VBA之精华,第9套教程是实用的典型案例讲解。如果您有了一定的VBA基础可以根据自己的需要,进行教程的选择。教程提供的程序源码文件就如一座大型的代码库支持着大家的工作。同时还有实用的资料送给学员。
VBA是面向对象编程的语言,博大精深。很多朋友咨询英语和VBA的关系,这套《VBA即用型代码手册(汉英)》集合了众多的案例,案例我用汉语和英语同时发布,学员从中可以更好的领会和掌握VBA中用到的一些英语。今日的内容:WORD_VBA文本框的添加、删除、写入及保存为html文件
Word Objects and Macro Examples
Sub mynzAddTextBox()
ActiveDocument.Shapes.AddTextBox Orientation:=msoTextOrientationHorizontal, _
Left:=100, Top:=180, Width:=300, Height:=100
End Sub
Sub mynzDeleteTextBox()
'我们需要检查 oShape 是否属于 msoShapeRectangle 类型,并且它的文本框是否包含书写位置
Dim oShape As Shape
If ActiveDocument.Shapes.Count > 0 Then
For Each oShape In ActiveDocument.Shapes
If oShape.AutoShapeType=msoShapeRectangle Then
If oShape.TextFrame.HasText=True Then
oShape.Delete
End If
End If
Next oShape
End If
End Sub
Sub mynzWriteInTextBox()
Dim oShape As Shape
If ActiveDocument.Shapes.Count > 0 Then
For Each oShape In ActiveDocument.Shapes
If oShape.AutoShapeType=msoShapeRectangle Then
If oShape.TextFrame.HasText=True Then
oShape.TextFrame.TextRange.InsertAfter "VBA Case"
Exit For
End If
End If
Next oShape
End If
End Sub
Sub mynzSaveMewithDateName()
'将当前活动文档保存为过滤后的 html,并以当前时间命名
Dim strTime As String
strTime=Format(Now, "hh-mm")
ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strTime, FileFormat:=wdFormatFilteredHTML
End Sub
【分享成果,随喜正能量】我20多年的VBA实践经验,全部浓缩在下面的各个教程中:
【分享成果,随喜正能量】总有一段时光,让我们深感痛苦,但不是所有的时光都这样,我们要学会在黑暗中,找到一丝光芒,这束光来自于内心,它会让我们重新找到好日子。。
<input name="fee" type="text" onkeyup="validates(this)">
<script type="text/javascript">
function validate(obj) {
obj.value=obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."
obj.value=obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是
obj.value=obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
obj.value=obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
obj.value=obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两位小数
}
</script>
*请认真填写需求信息,我们会在24小时内与您取得联系。