DOM基礎教程之使用DOM設定文字框,dom基礎教程

來源:互聯網
上載者:User

DOM基礎教程之使用DOM設定文字框,dom基礎教程

1.控制使用者輸入的字元個數

對於單行文字框和密碼輸入框,可以利用maxlength屬性控制使用者輸入的字元個數。
對於多行文本,maxlength為自訂屬性,其值最多輸入的字元的個數,在onkeypress事件發生時則調運返回LessThan()函數傳回值,函數如下

<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onekeypress ="return LessThan(this);"></textarea>
詳細代碼

複製代碼 代碼如下:
<script language="javascript">
function LessThan(oTextArea){
    //返迴文本框字元個數是否符號要求的boolean值
    return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
</script>
<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>

2.設定滑鼠經過自動選擇文本

首先是滑鼠經過時自動聚焦 onmouseover = "this.focus"

其次是 onfocus = "this.select()"
代碼執行個體:

複製代碼 代碼如下:
<form method="post" name="form1" id="form1" action="addInfo.aspx">
<input type="text" name="name" id="name" class="txt" value="姓名" onmouseover="this.focus()" onfocus="this.select()">
</form>

對於多個代碼執行個體,可以使用以下代碼進行聚焦

複製代碼 代碼如下:
<script type="text/javascript">
            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>
            <form method="post" name="myForm1" action="addInfo.aspx">
                <p>
                    <label for="name">請輸入您的姓名:</label>
                    <input type="text" name="name" id="name" class="txt" value="姓名">
                </p>
                <p>
                    <label for="passwd">請輸入您的密碼:</label>
                    <input type="password" name="passwd" id="passwd" class="txt">
                </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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.