本框是表單中與用戶打交道最多的元素之一,它包括單行文本框<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小時內與您取得聯系。