Event事件之鍵盤事件

1. onkeydown

在用戶按下一個鍵盤按鍵時發生

提示

與 onkeydown 事件相關聯的事件觸發次序:
1. onkeydown
2. onkeypress
3. onkeyup

HTML標籤支持

除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。

瀏覽器支持

google IE firefox safari opera
true true true true true

語法

ElementObject.onkeydown = function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<input id='input' type="text"/>
<textarea id='div'></textarea>
</body>
<script>
    input.onkeydown = function(){
        dataBound(this,div);
    }

    function dataBound(Ibound,Ybound){
        var YNodeNmae = Ybound.nodeName.toLowerCase();

        // 如果Ybound 不是 input 標籤 就給文本 否者給value值
        if(!(YNodeNmae == "input")){
            Ybound.innerText = Ibound.value;
        }else{
            Ybound.value = Ibound.value;
        }
    }//將 Ibound 的值給 Ybound
</script>
</html>

2. onkeypress

在鍵盤按鍵被按下並釋放一個鍵時發生

提示

與 onkeydown 事件相關聯的事件觸發次序:
1. onkeydown
2. onkeypress
3. onkeyup

HTML標籤支持

除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。

瀏覽器支持

google IE firefox safari opera
true true true true true

注意

在所有瀏覽器中 onkeypress 事件不是適用於所有按鍵(如: ALT, CTRL, SHIFT, ESC)。監聽一個用戶是否按下按鍵請使用 onkeydown 事件,所有瀏覽器都支持 onkeydown 事件。

語法

ElementObject.onkeypress = function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<input id='input' type="text"/>
<input id='div'/>
</body>
<script>
    input.onkeypress = function(){
        dataBound(this,div);
    }

    function dataBound(Ibound,Ybound){
        var YNodeNmae = Ybound.nodeName.toLowerCase();

        // 如果Ybound 不是 input 標籤 就給文本 否者給value值
        if(!(YNodeNmae == "input")){
            Ybound.innerText = Ibound.value;
        }else{
            Ybound.value = Ibound.value;
        }
    }//將 Ibound 的值給 Ybound
</script>
</html>

3. onkeyup

在鍵盤按鍵被鬆開時發生

提示

與 onkeydown 事件相關聯的事件觸發次序:
1. onkeydown
2. onkeypress
3. onkeyup

HTML標籤支持

除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。

瀏覽器支持

google IE firefox safari opera
true true true true true

注意

在所有瀏覽器中 onkeypress 事件不是適用於所有按鍵(如: ALT, CTRL, SHIFT, ESC)。監聽一個用戶是否按下按鍵請使用 onkeydown 事件,所有瀏覽器都支持 onkeydown 事件。

語法

ElementObject.onkeyup = function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<input id='input' type="text"/>
<div id='div'></div>
</body>
<script>
    input.onkeyup = function(){
        dataBound(this,div);
    }

    function dataBound(Ibound,Ybound){
        var YNodeNmae = Ybound.nodeName.toLowerCase();

        // 如果Ybound 不是 input 標籤 就給文本 否者給value值
        if(!(YNodeNmae == "input")){
            Ybound.innerText = Ibound.value;
        }else{
            Ybound.value = Ibound.value;
        }
    }//將 Ibound 的值給 Ybound
</script>
</html>

文檔內容出自 W3cSchool和菜鳥教程, 如需查看更詳細的有關內容 請登錄 http://www.w3school.com.cn/http://www.runoob.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章