解释此代码 jQuery

explain about this code jquery

本文关键字:jQuery 代码 解释      更新时间:2023-09-26

我发现这个代码从扫描条形码中获取值

$(document).ready(function() {
    $(document).focus();
    var coba=[];
    $(document).on('keypress',function(e){
        coba.push(String.fromCharCode(e.which));                                    
        if (coba[coba.length-1]=="'r") {
          console.log(coba.join(''));  
          simpan(coba.join(''));
          coba = [];
        };
    }); 
});

任何人都可以解释一下吗?

首先,我建议您访问 jquery.com 并查看他们的 api 文档和/或他们的学习中心。

请参阅下面嵌入的评论。这不会专门处理条形码

//this is waiting until the browser has loaded the page and all the content
// the elements in the content are considered the DOM.
$(document).ready(function() {
    //this sets the focus to the window, it acts as if you had clicked
    // on a blank spot of the window.
    $(document).focus();
    //this sets up an empty array to hold characters that are being typed
    var coba=[];
    //this sets up the page so that, when a key is pressed it does something
    //the anonymous function below is is executed when a key is pressed
    $(document).on('keypress',function(e){
        //adds the character that was pressed to the array
        coba.push(String.fromCharCode(e.which));          
        //if the return key was pressed                          
        if (coba[coba.length-1]=="'r") {
          //print out the characters that were pressed on the browser console
          console.log(coba.join(''));  
          //this passes the string that was typed to a function
          // to a function named simpan -- can't tell you what that is
          // because it isn't a browser function.  Probably in a library
          simpan(coba.join(''));
          // empty out the array to wait for a new string to be typed
          coba = [];
        };
    }); 
});

如果这与读取条形码有关,则可能发生的情况是连接到系统的条形码扫描仪就像键盘一样。simpan 函数可能由 javascript 库提供,甚至可能由硬件制造商提供。

祝你好运。