javascript使用事件,javascript事件

來源:互聯網
上載者:User

javascript使用事件,javascript事件

        p{            background: gray;            color: white;            padding:10px;            margin:5px;            border: thin solid black;        }        span{            background: white;            color: black;            padding: 2px;            cursor:pointer;        }        a{            background:gray;            color:white;            padding: 10px;            border: thin solid black;        }        table{            margin: 5px;            border-collapse: collapse;        }        th,td{            padding: 4px;        }
1)在元素上使用某個以on開頭的屬性——內聯處理事件;

    <p onmouseover="this.style.background='white';this.style.color='black'"       onmouseout="this.style.removeProperty('color');this.style.removeProperty('background')">        遠地本著“構築誠信,永續發展”的理念為客戶提供專業的理財服務、財富管理以及產品方案推薦。        遠地都進來看看撒將通過自身的專業優勢和有效資訊交流平台,為資金富裕方和資金需求方打造一個專業,誠信,共贏,睿智的服務平台,        協助客戶實現穩定、安全的財富增值,協助更多優秀的中小型企業融資成功。    </p>
2)定義函數,並用它的名稱作為以on開頭的屬性裡的值——用函數處理事件;

<p onmouseover="handleMouseOver(this)"   onmouseout="handleMouseOut(this)">    遠地本著“構築誠信,永續發展”的理念為客戶提供專業的理財服務、財富管理以及產品方案推薦。    遠地都進來看看撒將通過自身的專業優勢和有效資訊交流平台,為資金富裕方和資金需求方打造一個專業,誠信,共贏,睿智的服務平台,    協助客戶實現穩定、安全的財富增值,協助更多優秀的中小型企業融資成功。</p><p onmouseover="handleMouseOver(this)"   onmouseout="handleMouseOut(this)">    遠地秉承“奉獻、拼搏、勤奮、團結奉獻”的職業精神,已為200多家企業包括國企、民企和院所提供了企業轉型升級、    戰略規劃設計、集團組建、組織管理、品質管理體系、人力資源管理體系、財務風險管控和企業綜合諮詢等服務。    並與100多家企業建立長期戰略夥伴的合作關係。。</p>
    <script type="text/javascript">        function handleMouseOver(elem){            elem.style.background='white';            elem.style.color='black';        }        function handleMouseOut(elem){            elem.style.removeProperty('background');            elem.style.removeProperty('color');        }    </script>
3.1)使用標準的DOM搜尋技巧,並用以on開頭的屬性指派一個函數——用DOM處理事件;

3.2)用代表該元素的HTMLElement對象上的addEventListener方法——用DOM處理事件;

<p id="block1">    遠地本著“構築誠信,永續發展”的理念為客戶提供專業的<span id="licai">理財</span>服務、財富管理以及產品方案推薦。    遠地都進來看看撒將通過自身的專業優勢和有效資訊交流平台,為資金富裕方和資金需求方打造一個專業,誠信,共贏,睿智的服務平台,    協助客戶實現穩定、安全的財富增值,協助更多優秀的中小型企業融資成功。</p><p id="block2">    遠地秉承“奉獻、拼搏、勤奮、團結奉獻”的職業精神,已為200多家企業包括國企、民企和院所提供了企業轉型升級、    戰略規劃設計、集團組建、組織管理、品質管理體系、人力資源管理體系、財務風險管控和企業綜合諮詢等服務。    並與100多家企業建立長期戰略夥伴的合作關係。。</p><pre name="code" class="html"><button id="pressme">點擊移除事件</button>


<script type="text/javascript"><span style="font-family:KaiTi_GB2312;">  //使用Dom構建事件處理</span>  var pElems=document.getElementsByTagName("p");    for(var i=0;i<pElems.length;i++){        pElems[i].onmouseover=handleMouseOver;        pElems[i].onmouseout=handleMouseOut;    }    function handleMouseOver(e){        e.target.style.background='white';        e.target.style.color='black';    }    function handleMouseOut(e){        e.target.style.removeProperty('background');        e.target.style.removeProperty('color');    }</script>
<script type="text/javascript"><span style="font-family:KaiTi_GB2312;"></span>//使用addEventListener和removeEventListener方法 var pElems=document.getElementsByTagName("p");    for(var i=0;i<pElems.length;i++){        pElems[i].addEventListener("mouseover",handleMouseOver);        pElems[i].addEventListener("mouseout",handleMouseOut);    }    document.getElementById("pressme").onclick=function(){        document.getElementById("block2").removeEventListener("mouseout",handleMouseOut);    }    function handleMouseOver(e){        e.target.style.background='white';        e.target.style.color='black';    }    function handleMouseOut(e){        e.target.style.removeProperty('background');        e.target.style.removeProperty('color');    }</script>
3.3)Event對象的函數和屬性

    type——事件的名稱(如mouseover);

    target——事件指向的元素;

    currentTarget——帶有當前被觸發事件監聽器的元素;

    eventPhase——事件生命週期的階段;

    bubbles——如果事件會在文檔裡冒泡則返回true,否則返回false;

    cancelable——如果事件帶有可撤銷的預設行為則返回true,否則返回false;

    timeStamp——事件的建立時間,如果事件不可用則為0;

    stopPropagation()——在當前元素的事件監聽器被觸發後終止事件在元素樹中的流動;

    stopImmediatePropagation()——立即終止事件在元素樹中的流動。當前元素上未被觸發的事件監聽器會被忽略;

    preventDefault()——防止瀏覽器執行與事件關聯的預設操作;

    defaultPrevented——如果調用過preventDefault()則返回true;

<script type="text/javascript">//<span style="font-family:KaiTi_GB2312;">使用type屬性</span> var pElems=document.getElementsByTagName("p");    for(var i=0;i<pElems.length;i++){        pElems[i].onmouseover=handleMouseEvent;        pElems[i].onmouseout=handleMouseEvent;    }    function handleMouseEvent(e){        if(e.type=="mouseover"){            e.target.style.background='white';            e.target.style.color='black';        }else{            e.target.style.removeProperty('background');            e.target.style.removeProperty('color');        }    }</script>
<script type="text/javascript"><span style="font-family:KaiTi_GB2312;">//</span>理解事件流:捕捉、目標、冒泡 var licai=document.getElementById("licai");    var textblock=document.getElementById("block1");    licai.addEventListener("mouseover",handleMouseEvent);    licai.addEventListener("mouseout",handleMouseEvent);    textblock.addEventListener("mouseover",handleDescendantEvent,true);    textblock.addEventListener("mouseout",handleDescendantEvent,true);    textblock.addEventListener("mouseover",handleBubbleMouseEvent,false);    textblock.addEventListener("mouseout",handleBubbleMouseEvent,false);    function handleBubbleMouseEvent(e){        if(e.type=="mouseover" && e.eventPhase==Event.BUBBLING_PHASE){            e.target.style.padding="5px";        }else if(e.type=="mouseout" && e.eventPhase==Event.BUBBLING_PHASE){            e.target.style.removeProperty('padding');        }    }    function handleDescendantEvent(e){        if(e.type=="mouseover" && e.eventPhase==Event.CAPTURING_PHASE){            e.target.style.border="thick solid red";            e.currentTarget.style.border="thick double black";        }else if(e.type=="mouseout" && e.eventPhase==Event.CAPTURING_PHASE){            e.target.style.removeProperty('border');            e.currentTarget.style.removeProperty('border');        }        //e.stopImmediatePropagation();        //e.stopPropagation();    }    function handleMouseEvent(e){        if(e.type=="mouseover"){            e.target.style.background='white';            e.target.style.color='black';        }else{            e.target.style.removeProperty('background');            e.target.style.removeProperty('color');        }    }</script>
    <a href="http://www.sina.com.cn">訪問新浪</a>    <a href="http://www.w3school.com.cn">訪問W3C</a>
<script type="text/javascript"><span style="font-family:KaiTi_GB2312;">//</span>撤銷預設的行為<span style="font-family:KaiTi_GB2312;"></span> function handleClick(e){        if(!confirm("您真的要訪問"+ e.target.href+" 嗎?")){            e.preventDefault();        }    }    var elems=document.querySelectorAll("a");    for(var i=0;i<elems.length;i++){        elems[i].addEventListener("click",handleClick,false);    }</script>
3.4)使用HTML事件

3.4.1)文檔事件(document對象)

      readystatechange——在readyState屬性的值發生變化時觸發;

3.4.2)視窗事件(window對象)

      onabort——在文檔或資源載入過程被終止時觸發;

      onafterprint——在已調用window.print()方法,但尚未給使用者提供列印選項時觸發;

      onbeforeprint——在使用者完成文檔列印後觸發;

      onerror——在文檔或資源的載入發生錯誤時觸發;

      onhashchangge——在錨部分發生變化時觸發;

      onload——在文檔或資源載入完成時觸發;

      onpopstate——觸發後提供一個關聯瀏覽器曆史的狀態物件;

      onresize——在視窗縮放時觸發;

      onunload——在文檔從視窗或瀏覽器中卸載時觸發;

3.4.3)滑鼠事件

     click——在點擊並釋放滑鼠鍵時觸發;

     dbclick——在兩次點擊並釋放滑鼠鍵時觸發;

     mousedown——在點擊滑鼠鍵時觸發;

     mouseenter——在游標移入元素或某個後代元素所佔據的螢幕地區時觸發;

     mouseleave——在游標移出元素或所有後代元素所佔據的螢幕地區時觸發;

     mousemove——當游標在元素上移動時觸發;

     mouseout——與mouseleave基本相同,除了當游標仍然在某個後代元素上時也會觸發;

     mouseover——與mouseenter基本相同,除了當游標仍然在某個後代元素上時也會觸發;

     mouseup——在釋放滑鼠鍵時觸發;

3.4.3.1)MouseEvent對象

        button——標明點擊的是哪個鍵。0是滑鼠主鍵,1中中鍵,2是右鍵;

        altKey——如果在事件觸發時按下了alt/option鍵則返回true;

        clientX——返回事件觸發時滑鼠相對於元素視口的X座標;

       clientY——返回事件觸發時滑鼠相對於元素視口的Y座標;

        screenX——返回事件觸發時滑鼠相對於螢幕座標系的X座標;

        screenY——返回事件觸發時滑鼠相對於螢幕座標系的Y座標;

        shiftKey——如果在事件觸發時按下了shift鍵則返回true;

        ctrlKey——如果在事件觸發時按下了Ctrl鍵則返回true;

<p id="block1">    遠地本著“構築誠信,永續發展”的理念為客戶提供專業的<span id="licai">理財</span>服務、財富管理以及產品方案推薦。    遠地都進來看看撒將通過自身的專業優勢和有效資訊交流平台,為資金富裕方和資金需求方打造一個專業,誠信,共贏,睿智的服務平台,    協助客戶實現穩定、安全的財富增值,協助更多優秀的中小型企業融資成功。</p><table border="1">    <tr>        <th>Type:</th><td id="eType"></td>    </tr>    <tr>        <th>X:</th><td id="eX"></td>    </tr>    <tr>        <th>Y:</th><td id="eY"></td>    </tr></table>
<script type="text/javascript">//使用MouseEvent對象響應滑鼠事件    var textblock=document.getElementById("block1");    var typeCell=document.getElementById("eType");    var xCell=document.getElementById("eX");    var yCell=document.getElementById("eY");    textblock.addEventListener("mouseover",handleMouseEvent,false);    textblock.addEventListener("mouseout",handleMouseEvent,false);    textblock.addEventListener("mousemove",handleMouseEvent,false);    function handleMouseEvent(e){        if(e.eventPhase==Event.AT_TARGET){            typeCell.innerHTML= e.type;            xCell.innerHTML= e.clientX;            yCell.innerHTML= e.clientY;        }        if(e.type=="mouseover"){            e.target.style.background='black';            e.target.style.color='white';        }else{            e.target.style.removeProperty('color');            e.target.style.removeProperty('background');        }    }</script>
3.4.4)鍵盤焦時間點事件

     blur——在元素失去焦點時觸發;

    focus——在元素獲得焦點時觸發;

    focusin——在元素即將獲得焦點時觸發;

    focusout——在元素即將失去焦點時觸發;

3.4.5)鍵盤事件

     keydown——在使用者按下某個鍵時觸發;

     keypress——在使用者按下並釋放某個鍵時觸發;

     keyup——在使用者釋放某個鍵時觸發;

3.4.5.1)KeyboardEvent對象

    char——返回按鍵代表的字元;

    key——返回所按的鍵;

    ctrlKey——如果在按鍵時Ctrl鍵處於按下狀態則返回true;

   shiftKey——如果在按鍵時shift鍵處於按下狀態則返回true;

   altKey——如果在按鍵時shift鍵處於按下狀態則返回true;

    repeat——如果該鍵一直處於按下狀態時則返回true;

   

<form>    <p>        <label for="fave">Fruit:<input autofocus id="fave" name="fave"/></label>    </p>    <p>        <label for="name">Name:<input autofocus id="name" name="name"/></label>    </p>    <button type="submit">提交</button>    <button type="reset">重設</button></form><br/><span id="message"></span>
<script type="text/javascript">    var inputElems=document.getElementsByTagName("input");    for(var i=0;i<inputElems.length;i++){        inputElems[i].onkeyup=handleKeyboardEvent;    }    function handleKeyboardEvent(e){        document.getElementById("message").innerHTML="Key pressed: "+ e.keyCode+" Char:"+String.fromCharCode(e.keyCode);    }</script>
3.4.6)表單事件

     submit——在表單提交時觸發;

     reset——在表單重設時觸發;

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

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