解释一下这段代码jquery

时间:2016-01-30 03:43:58

标签: javascript jquery

我发现此代码可以从扫描条形码中获取值

{{1}}

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

1 个答案:

答案 0 :(得分:1)

首先,我建议您访问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库提供,甚至可能由硬件制造商提供。

祝你好运。