Input的特殊事件onpopertychange和oninput

來源:互聯網
上載者:User

在很多情況下,我們需要監測輸入框,比如自動提示等。我們熟知的一些input的按鍵事件並不能滿足我們的需求,特別是在IME開啟的狀態下,三個瀏覽器的具體問題如下: 

IE:觸發keydown和keyup,不觸發keypress。能夠獲得輸入值。 

Firefox::觸發keydown和keypress,不觸發keyup。輸入值未能獲得。在斷行符號後會觸發keyup,可獲得輸入值。 

Opera:keydown、keypress和keyup都不觸發,輸入值也未能獲。 

幸好我們還有一些秘密武器onpopertychange和oninput。onpopertychange事件只在IE下才有效,oninput事件則支援FF/Opera/Safari/NetScape。這2個事件在輸入開啟狀態下仍然可以捕獲輸入框的輸入動作及其值。

代碼示範:

 <input type="text" id="inpA" />

 <input type="text" id="inpB" />

 <script language="javascript" type="text/javascript">

 //onpopertychange IE only;

 $("inpA").onpropertychange=function(){

     change();

 }

 //oninput for FF/Opera/Safari/NetScape

 $("inpA").oninput=function(){

    change();

}

function change(){

    $("inpB").value = $("inpA").value;

}

function $(id){

    return document.getElementById(id);

}

</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.