input標籤的accept屬性、JQuery綁定keyDown事件

一. input標籤的accept屬性

  當咱們上傳文件或者註冊上傳頭像時,咱們能夠通常都是使用:jquery

<input type="file" id="my_file">

  可是這樣的話,全部文件都會顯示出來,這裏以上傳頭像爲例,一點擊選擇文件,全部跟圖片無關的文件也會顯示出來:性能

                              

  這時能夠給input標籤增長一個accept屬性,讓它只顯示圖片相關的文件:spa

<input type="file" id="my_file" accept="image/*" >

  如今再來看看效果:3d

二. JQuery綁定keyDown事件

  通常登陸時,輸完以後點擊回車便可登陸,這是綁定了事件,咱們能夠用標籤選擇器來給全部input標籤綁定keyDown事件。code

  首先提一下window.event事件,event表明事件的狀態,例如觸發event對象的元素、鼠標的位置及狀態、按下的鍵等等。event對象只在事件發生的過程當中纔有效。 event的某些屬性只對特定的事件有意義。好比,fromElement 和 toElement 屬性只對 onmouseover 和 onmouseout 事件有意義。 event事件屬性:對象

altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, keyCode, offsetX, offsetY, 
propertyName, returnValue, screenX, screenY, shiftKey, srcElement, srcFilter, toElement, type, x, y

  詳情點擊--》》API文檔blog

$('input').keydown(function () {
        let e = window.event||arguments[0];
        #回車鍵ascii碼爲13
         if (e.keyCode == 13){
              alert('你按下回車了!!!')
    });

  實際上event事件還有一個event.which事件對象,針對鍵盤和鼠標事件,這個屬性能肯定你到底按的是哪一個鍵。官方推薦用 event.which 來監視鍵盤輸入。更多細節請參閱: event.charCode on the MDC.seo

  用event.which時只需將e.keyCode改成e.which便可:事件

$('input').keydown(function () {
        let e = window.event||arguments[0];
        #回車鍵ascii碼爲13
         if (e.which == 13){
              alert('你按下回車了!!!')
    });

  鍵盤事件:https://www.jquery123.com/keydown/圖片

  小例子,給body綁定按鍵事件,按下Backspace鍵返回上一級頁面:

$('body').keydown(function () {
        let e = window.event||arguments[0];
        if(e.keyCode==8){
            history.back();
        }
    });
相關文章
相關標籤/搜索