Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery按键复制_Jquery_Html_Keypress - Fatal编程技术网

jQuery按键复制

jQuery按键复制,jquery,html,keypress,Jquery,Html,Keypress,我试图显示用户使用jQuery键入的内容以检测按下的键 例如,如果用户按下“a”,则显示“a”。当按下“z”键时,将显示“az”,依此类推 我的代码: <script> $(document).ready(function() { var res = ""; $("*").keypress(function( event ) { res = res + String.fromCharCode(event.which);

我试图显示用户使用jQuery键入的内容以检测按下的键

例如,如果用户按下“a”,则显示“a”。当按下“z”键时,将显示“az”,依此类推

我的代码:

<script>
    $(document).ready(function() {
        var res = "";

        $("*").keypress(function( event ) {
            res = res + String.fromCharCode(event.which);
            $("#go2").html(res);
        });
    });
</script>


<p id="go2"></p>

$(文档).ready(函数(){
var res=“”;
$(“*”).keypress(函数(事件){
res=res+String.fromCharCode(event.which);
$(“#go2”).html(res);
});
});

显示
String.fromCharCode(event.which)
而不是
res
正确显示按下的键,但是当前代码与键重复


当我按下“a”键时,显示“aaa”。按“z”等新键,然后显示“aaazzz”。为什么钥匙要复制

问题出在您的
*
选择器中,它同时应用于

只需增加对
主体的特异性
就会导致复制消失:

$(文档).ready(函数(){
var res=“”;
$(“正文”)。按键(功能(事件){
res=res+String.fromCharCode(event.which);
$(“#go2”).html(res);
});
});


非常感谢您!!我早该知道那些简单的
*
会回来缠着我。