本文实例讲述了JS生成一维码(条形码)功能的方法。分享给大家供大家参考,具体如下:
1、js代码:
' + code + '';
}
function bar2html(s) {
for(var pos=0,sb=[]; pos');
}
return sb.join('');
}
function code128Detect(code) {
if (/^[0-9]+$/.test(code)) return 'C';
if (/[a-z]/.test(code)) return 'B';
return 'A';
}
function parseBarcode(barcode,barcodeType) {
var bars = [];
bars.add = function(nr) {
var nrCode = BARS[nr];
this.check = this.length==0 ? nr : this.check + nr*this.length;
this.push( nrCode || ("UNDEFINED: "+nr+"->"+nrCode) );
};
bars.add(START_BASE + barcodeType.charCodeAt(0));
for(var i=0; i106) throw new Error("Unrecognized character ("+code+") at position "+i+" in code '"+barcode+"'.");
bars.add( converted );
}
bars.push(BARS[bars.check % 103],BARS[STOP]);
return bars;
}
var fromType = {
A: function(charCode) {
if (charCode>=0 && charCode<32) return charCode+64;
if (charCode>=32 && charCode<96) return charCode-32;
return charCode;
},B: function(charCode) {
if (charCode>=32 && charCode<128) return charCode-32;
return charCode;
},C: function(charCode) {
return charCode;
}
};
//--| Export
exports.code128 = code128;
})();
/*
showDiv:代表需要显示的divID,
textVlaue : 代表需要生成的值,
barcodeType:代表生成类型(A、B、C)三种类型
*/
function createBarcode(showDiv,textValue,barcodeType){
var divElement = document.getElementById(showDiv);
divElement.innerHTML = code128(textValue,barcodeType);
}
2.css代码如下:
3.HTML代码如下: