Javascript 解释jquery中的代码
我发现这个代码可以从扫描条形码中获取值Javascript 解释jquery中的代码,javascript,jquery,Javascript,Jquery,我发现这个代码可以从扫描条形码中获取值 $(document).ready(function() { $(document).focus(); var coba=[]; $(document).on('keypress',function(e){ coba.push(String.fromCharCode(e.which)); if (coba[coba.lengt
$(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库提供,甚至可能由硬件制造商提供
祝你好运。你到底在问什么?谷歌是你的朋友。从谷歌搜索每个函数的含义开始,然后从那里开始。