当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


jQuery event.metaKey用法及代码示例


event.metaKeyjQuery 中的属性用于识别元键是否被按下。下面讨论两个例子。
用法:

event.metaKey

返回值:如果键是元键,则返回“true”,否则返回“false”。

示例 1:在此示例中,输入 <input> 元素并查看结果。


<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        jQuery event.metaKey Property 
    </title> 
  
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
      
    <p id="GFG_UP"></p> 
  
    <input type="text" 
        onkeydown="Geeks(event)" /> 
  
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var elUp = document.getElementById("GFG_UP"); 
        var elDown = document.getElementById("GFG_DOWN"); 
        elUp.innerHTML = "JQuery | event.metaKey property"; 
  
        function Geeks(event) { 
            if (event.metaKey) { 
                elDown.innerHTML =  
                    "The META key was pressed!"; 
            } else { 
                elDown.innerHTML =  
                    "The META key was NOT pressed!"; 
            } 
        }  
    </script> 
</body> 
  
</html> 

输出:

示例 2:从文档中的任意位置按任意按钮即可查看结果。


<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        JQuery event.metaKey Property 
    </title> 
  
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
      
    <p id="GFG_UP"></p> 
  
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var elUp = document.getElementById("GFG_UP"); 
        var elDown = document.getElementById("GFG_DOWN"); 
        elUp.innerHTML = "JQuery | event.metaKey property"; 
          
        $('html').click(function (event) { 
            if (event.metaKey) { 
                elDown.innerHTML =  
                    "The META key was pressed!"; 
            } else { 
                elDown.innerHTML =  
                    "The META key was NOT pressed!"; 
            } 
        });  
    </script> 
</body> 
  
</html> 

输出:



相关用法


注:本文由纯净天空筛选整理自PranchalKatiyar大神的英文原创作品 jQuery event.metaKey Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。